@@ -2333,26 +2333,60 @@ async function initializeDiscordApp() {
23332333async function getDiscordUserInfo ( ) {
23342334 console . log ( "Attempting to get Discord user info..." ) ;
23352335 if ( window . isDiscord ) {
2336- const { code } = await window . discordSdk . commands . authorize ( {
2337- client_id : window . discordSdk . clientId ,
2338- response_type : "code" ,
2339- scope : [ "identify" ] ,
2340- prompt : "none"
2341- } ) ;
2336+ try {
2337+ const { code } = await window . discordSdk . commands . authorize ( {
2338+ client_id : window . discordSdk . clientId ,
2339+ response_type : "code" ,
2340+ scope : [ "identify" ] ,
2341+ prompt : "none"
2342+ } ) ;
23422343
2343- const response = await fetch ( '/token' , {
2344- method : 'POST' ,
2345- body : JSON . stringify ( { code } ) ,
2346- } ) ;
2347- const data = await response . json ( ) ;
2348- const token = data . access_token ;
2349- const auth = await window . discordSdk . commands . authenticate ( {
2350- access_token : token ,
2351- } ) ;
2352- console . log ( "User ID:" , auth . user . id ) ;
2353- console . log ( "Username:" , auth . user . username ) ;
2354- window . discordUser = auth . user ;
2355- localStorage . setItem ( 'discordUser' , JSON . stringify ( auth . user ) ) ;
2344+ const response = await fetch ( '/api/token' , {
2345+ method : 'POST' ,
2346+ body : JSON . stringify ( { code } ) ,
2347+ } ) ;
2348+ const data = await response . json ( ) ;
2349+ const token = data . access_token ;
2350+ const auth = await window . discordSdk . commands . authenticate ( {
2351+ access_token : token ,
2352+ } ) ;
2353+ if ( ! auth . user || ! auth . user . id || ! auth . user . username ) {
2354+ throw new Error ( 'Missing Discord user info' ) ;
2355+ }
2356+ console . log ( "User ID:" , auth . user . id ) ;
2357+ console . log ( "Username:" , auth . user . username ) ;
2358+ window . discordUser = auth . user ;
2359+ localStorage . setItem ( 'discordUser' , JSON . stringify ( auth . user ) ) ;
2360+ } catch ( err ) {
2361+ console . error ( "Discord User Authorization Failed:" , err ) ;
2362+ $ . dialog ( {
2363+ title : '<span class="modalTitle">Error</span>' ,
2364+ content : '<span class="modalText">Discord User Authorization Failed, some features may not work correctly.</span>' ,
2365+ type : 'red' ,
2366+ theme : window . game . theme ,
2367+ animation : 'top' ,
2368+ closeAnimation : 'top' ,
2369+ animateFromElement : false ,
2370+ boxWidth : 'min(400px, 80%)' ,
2371+ draggable : false ,
2372+ useBootstrap : false ,
2373+ typeAnimated : true ,
2374+ backgroundDismiss : true ,
2375+ buttons : {
2376+ retry : {
2377+ text : 'Retry' ,
2378+ btnClass : 'btn-blue' ,
2379+ action : function ( ) {
2380+ getDiscordUserInfo ( ) ;
2381+ }
2382+ } ,
2383+ close : {
2384+ text : 'Close' ,
2385+ btnClass : 'btn-default'
2386+ }
2387+ }
2388+ } ) ;
2389+ }
23562390 }
23572391}
23582392
@@ -2365,7 +2399,7 @@ async function sendDiscordMessageUpdate() {
23652399 userId : window . discordUser . id ,
23662400 hiddenMode : window . gameSesh . hideBlanks ,
23672401 cardArtUrl : window . mtgCard . image_uris ? window . mtgCard . image_uris . art_crop : ( window . mtgCard . card_faces ? window . mtgCard . card_faces [ 0 ] . image_uris . art_crop : '' ) ,
2368- lives : window . game . mode == 'daily' ? window . game . daily . lives - window . gameSesh . wrongGuess . length : undefined ,
2402+ lives : window . game . mode == 'daily' ? window . game . daily . lives - window . gameSesh . wrongGuess . length : undefined ,
23692403 guessProgress : window . gameSesh . guessProgress
23702404 } )
23712405 } ) ;
@@ -2374,6 +2408,20 @@ async function sendDiscordMessageUpdate() {
23742408 }
23752409}
23762410
2411+ async function getDiscordLaunchConfig ( ) {
2412+ if ( window . isDiscord ) {
2413+ const { channelId, userId } = window . discordSdk ;
2414+ const res = await fetch ( `/api/config?channelId=${ channelId } &userId=${ userId } ` ) ;
2415+ const intent = await res . json ( ) ;
2416+
2417+ if ( intent . mode === 'daily' ) {
2418+ return ( 'daily' ) ;
2419+ } else {
2420+ console . log ( "Loading standard Befuddle..." ) ;
2421+ }
2422+ }
2423+ }
2424+
23772425//start script
23782426$ ( document ) . ready ( function ( ) {
23792427 console . log ( 'Befuddle version: ' + befuddleAppVersion ) ;
@@ -2550,16 +2598,21 @@ $(document).ready(function () {
25502598 //set game mode
25512599 window . game . mode = '' ;
25522600
2601+ //get Discord activity launch configs
2602+ let discordLaunchParam = getDiscordLaunchConfig ( ) ;
2603+
25532604 //specific link to card
25542605 if ( getParameterByName ( 'cardId' ) ) {
25552606 window . game . mode = 'free' ;
25562607 window . gameSesh . end = true ;
25572608 loadGame ( ) ;
2558- } else if ( getParameterByName ( 'daily' ) ||
2559- getParameterByName ( 'custom_id' ) === 'daily'
2609+ } else if ( getParameterByName ( 'daily' )
25602610 ) {
25612611 console . log ( 'Daily link detected' ) ;
25622612 startDaily ( ) ;
2613+ } else if ( discordLaunchParam === 'daily' ) {
2614+ console . log ( 'Discord daily trigger detected' ) ;
2615+ startDaily ( ) ;
25632616 } else {
25642617 mainMenuDisplay ( ) ;
25652618 }
0 commit comments