diff --git a/src/core/api/modules/client.ts b/src/core/api/modules/client.ts index d2e2868..45dea53 100644 --- a/src/core/api/modules/client.ts +++ b/src/core/api/modules/client.ts @@ -135,8 +135,8 @@ export class ODClientManager { }) } } - /**Log-in with a discord auth token. */ - login(): Promise { + /**Log-in with a discord auth token. Rejects returns `false` using 'softErrors' on failure. */ + login(softErrors?:boolean): Promise { return new Promise(async (resolve,reject) => { if (!this.initiated) reject("Client isn't initiated yet!") if (!this.token) reject("Client doesn't have a token!") @@ -158,7 +158,8 @@ export class ODClientManager { this.#debug.debug("Finished discord.js client.login()") this.loggedIn = true }catch(err){ - if (err.message.toLowerCase().includes("used disallowed intents")){ + if (softErrors) return resolve(false) + else if (err.message.toLowerCase().includes("used disallowed intents")){ process.emit("uncaughtException",new ODSystemError("Used disallowed intents")) }else if (err.message.toLowerCase().includes("tokeninvalid") || err.message.toLowerCase().includes("an invalid token was provided")){ process.emit("uncaughtException",new ODSystemError("Invalid discord bot token provided")) diff --git a/src/core/cli/quickSetup.ts b/src/core/cli/quickSetup.ts index c27e17f..2e7d283 100644 --- a/src/core/cli/quickSetup.ts +++ b/src/core/cli/quickSetup.ts @@ -68,11 +68,11 @@ async function renderQuickSetupWelcome(backFn:() => api.ODPromiseVoid){ }).promise if (answer.canceled) return await backFn() - else if (answer.selectedIndex == 0) await renderQuickSetupDevPortalInfo(async () => {await renderQuickSetupWelcome(backFn)}) + else if (answer.selectedIndex == 0) await renderQuickSetupDevPortal(async () => {await renderQuickSetupWelcome(backFn)}) } -async function renderQuickSetupDevPortalInfo(backFn:() => api.ODPromiseVoid){ - renderHeader("⏱️ Open Ticket Quick Setup: Dev Portal & Experience") +async function renderQuickSetupDevPortal(backFn:() => api.ODPromiseVoid){ + renderHeader("⏱️ Open Ticket Quick Setup: Discord Bot & Developer Portal") terminal.bold.blue("(Step 1) Have you already created a Discord bot to use for Open Ticket?\n") @@ -90,20 +90,20 @@ async function renderQuickSetupDevPortalInfo(backFn:() => api.ODPromiseVoid){ }).promise if (answer.canceled) return await backFn() - else if (answer.selectedIndex == 0) await backFn() //TODO NEXT QUESTION - else if (answer.selectedIndex == 1) await renderQuickSetupDevPortalGuide(0,async () => {await renderQuickSetupDevPortalInfo(backFn)}) - else if (answer.selectedIndex == 2) await renderQuickSetupDevPortalGuide(1,async () => {await renderQuickSetupDevPortalInfo(backFn)}) + else if (answer.selectedIndex == 0) await renderQuickSetupBotToken(async () => {await renderQuickSetupDevPortal(backFn)}) + else if (answer.selectedIndex == 1) await renderQuickSetupDevPortalGuide(0,async () => {await renderQuickSetupDevPortal(backFn)}) + else if (answer.selectedIndex == 2) await renderQuickSetupDevPortalGuide(1,async () => {await renderQuickSetupDevPortal(backFn)}) } async function renderQuickSetupDevPortalGuide(variation:0|1,backFn:() => api.ODPromiseVoid){ - renderHeader("⏱️ Open Ticket Quick Setup: Dev Portal & Experience") + renderHeader("⏱️ Open Ticket Quick Setup: Discord Bot & Developer Portal") if (variation == 0){ terminal.bold.blue("(Step 1.1) You've mentioned that you don't know how to create a Discord bot.\n\n") terminal.gray("Please visit the following URL for a step-by-step guide on how to create a Discord bot.\nIf it still doesn't work, join our Discord server and we will help you further!\n"+ansis.magenta("=> https://otdocs.dj-dj.be/docs/guides/get-started#bot\n\n")) }else{ terminal.bold.blue("(Step 1.2) You've mentioned that you've never seen Discord bot before.\n\n") - terminal.gray("How did you even download Open Ticket 🤪? But still, we have a step-by-step guide on how to create a Discord bot.\nIf it still doesn't work, join our Discord server and we will help you further!\n"+ansis.magenta("=> https://otdocs.dj-dj.be/docs/guides/get-started#bot\n\n")) + terminal.gray("How did you even download Open Ticket 🤪? But all jokes aside, we have a step-by-step guide on how to create a Discord bot.\nIf it still doesn't work, join our Discord server and we will help you further!\n"+ansis.magenta("=> https://otdocs.dj-dj.be/docs/guides/get-started#bot\n\n")) } const answer = await terminal.singleColumnMenu([ @@ -119,3 +119,60 @@ async function renderQuickSetupDevPortalGuide(variation:0|1,backFn:() => api.ODP return await backFn() } + +async function quickSetupLogin(token:string): Promise { + const client = new api.ODClientManager(opendiscord.debug) + client.token = token + client.intents.push("Guilds","GuildMessages","DirectMessages","GuildEmojisAndStickers","GuildMembers","MessageContent","GuildWebhooks","GuildInvites") + client.privileges.push("MessageContent","GuildMembers") + client.partials.push("Channel","Message") + client.permissions.push("AddReactions","AttachFiles","CreatePrivateThreads","CreatePublicThreads","EmbedLinks","ManageChannels","ManageGuild","ManageMessages","ChangeNickname","ManageRoles","ManageThreads","ManageWebhooks","MentionEveryone","ReadMessageHistory","SendMessages","SendMessagesInThreads","UseApplicationCommands","UseExternalEmojis","ViewAuditLog","ViewChannel") + client.initClient() + + //client login + return new Promise(async (resolve) => { + try{ + client.readyListener = async () => { + client.activity.setStatus("custom","Configuring Open Ticket...","idle",true) + resolve(client) + } + const success = await client.login(true) + if (!success) resolve(null) + }catch(err){ + resolve(null) + } + }) +} + +async function renderQuickSetupBotToken(backFn:() => api.ODPromiseVoid){ + renderHeader("⏱️ Open Ticket Quick Setup: Bot Token") + + terminal.bold.blue("(Step 2) Please insert the token of your discord bot.\n") + terminal.gray("It will be safely stored in the 'config/general.json' file.\n\n> ") + + const answer = await terminal.inputField({ + style:terminal.white, + hintStyle:terminal.gray, + cancelable:true + }).promise + + if (typeof answer != "string") return await backFn() + else{ + const result = await quickSetupLogin(answer) + if (!result){ + //login failure + terminal.red.bold("\n\n❌ Something went wrong with logging into the bot.\n") + terminal.gray("Please try again and check your token for any mistakes.\nAlso make sure the permissions and priviliged gateaway intents are configured correctly in the developer panel.") + await utilities.timer(3000) + //retry + await renderQuickSetupBotToken(backFn) + }else{ + //login success + terminal.green.bold("\n\n✅ Succesfully logged into the bot.\n") + terminal.gray("Your bot should be online with the status 'Configuring Open Ticket...'.") + await utilities.timer(3000) + //continue + //TODO + } + } +}