@@ -46,44 +46,52 @@ public string GetAnonymousId(int accountId)
4646
4747 public async Task LinkAccount ( string state , string code )
4848 {
49- int accountId ;
50- if ( ! userIds . TryGetValue ( state , out accountId ) ) {
51- Trace . TraceWarning ( "Invalid state " + state ) ;
52- return ;
53- }
49+ try
50+ {
51+ int accountId ;
52+ if ( ! userIds . TryGetValue ( state , out accountId ) )
53+ {
54+ Trace . TraceWarning ( "Invalid state " + state ) ;
55+ return ;
56+ }
5457
55- var request = new HttpRequestMessage ( HttpMethod . Post , "https://discordapp.com/api/oauth2/token" ) ;
56- request . Content = new FormUrlEncodedContent ( new Dictionary < string , string > {
57- { "client_id" , GlobalConst . ZeroKDiscordID } ,
58- { "client_secret" , new Secrets ( ) . GetDiscordClientSecret ( ) } ,
59- { "grant_type" , "authorization_code" } ,
60- { "code" , code } ,
61- { "redirect_uri" , GetRedirectURL ( ) } ,
62- { "scope" , "identify" } ,
63- } ) ;
58+ var request = new HttpRequestMessage ( HttpMethod . Post , "https://discordapp.com/api/oauth2/token" ) ;
59+ request . Content = new FormUrlEncodedContent ( new Dictionary < string , string > {
60+ { "client_id" , GlobalConst . ZeroKDiscordID } ,
61+ { "client_secret" , new Secrets ( ) . GetDiscordClientSecret ( ) } ,
62+ { "grant_type" , "authorization_code" } ,
63+ { "code" , code } ,
64+ { "redirect_uri" , GetRedirectURL ( ) } ,
65+ { "scope" , "identify" } ,
66+ } ) ;
6467
65- var response = await new HttpClient ( ) . SendAsync ( request ) ;
66- response . EnsureSuccessStatusCode ( ) ;
68+ var response = await new HttpClient ( ) . SendAsync ( request ) ;
69+ response . EnsureSuccessStatusCode ( ) ;
6770
68- var payload = JObject . Parse ( await response . Content . ReadAsStringAsync ( ) ) ;
69- var token = payload . Value < string > ( "access_token" ) ;
71+ var payload = JObject . Parse ( await response . Content . ReadAsStringAsync ( ) ) ;
72+ var token = payload . Value < string > ( "access_token" ) ;
7073
71- var discord = new DiscordSocketClient ( ) ;
72- await discord . StartAsync ( ) ;
73- await discord . LoginAsync ( TokenType . Bearer , token ) ;
74- var discordId = discord . CurrentUser . Id ;
75- using ( var db = new ZkDataContext ( ) )
76- {
77- var existing = db . Accounts . FirstOrDefault ( x => x . DiscordID == discordId ) ;
78- if ( existing != null )
74+ var discord = new DiscordSocketClient ( ) ;
75+ await discord . StartAsync ( ) ;
76+ await discord . LoginAsync ( TokenType . Bearer , token ) ;
77+ var discordId = discord . CurrentUser . Id ;
78+ using ( var db = new ZkDataContext ( ) )
7979 {
80- Trace . TraceInformation ( "Unlinking discord for Account " + existing . Name ) ;
81- existing . DiscordID = ( decimal ? ) null ;
80+ var existing = db . Accounts . FirstOrDefault ( x => x . DiscordID == discordId ) ;
81+ if ( existing != null )
82+ {
83+ Trace . TraceInformation ( "Unlinking discord for Account " + existing . Name ) ;
84+ existing . DiscordID = ( decimal ? ) null ;
85+ db . SaveChanges ( ) ;
86+ }
87+ Trace . TraceInformation ( "Linking discord id " + discordId + " to Account " + existing . Name ) ;
88+ db . Accounts . FirstOrDefault ( x => x . AccountID == accountId ) . DiscordID = discordId ;
8289 db . SaveChanges ( ) ;
8390 }
84- Trace . TraceInformation ( "Linking discord id " + discordId + " to Account " + existing . Name ) ;
85- db . Accounts . FirstOrDefault ( x => x . AccountID == accountId ) . DiscordID = discordId ;
86- db . SaveChanges ( ) ;
91+ }
92+ catch ( Exception ex )
93+ {
94+ Trace . TraceError ( "Error linking discord ID " + ex ) ;
8795 }
8896 }
8997 }
0 commit comments