Created
October 1, 2024 05:55
-
-
Save Pkmmte/65833109d652d6a15f062e66b3a9a2a7 to your computer and use it in GitHub Desktop.
# Discord Activity Entry Command Fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set this to true if you run into issues or want to see app data logged. | |
const Debug = false | |
// Set this as your Discord Bot Token and Client ID if you don't want to pass them as arguments. | |
const BotToken = '' | |
const ClientId = '' | |
// Runs this script. | |
start().then(console.log).catch(console.error) | |
/** | |
* Fixes the Entry Command in your Discord App. | |
* | |
* Run this in your terminal: | |
* ```bash | |
* node entry-fix.js <DISCORD_CLIENT_ID> <DISCORD_TOKEN> | |
* ``` | |
* | |
* Replace `<DISCORD_CLIENT_ID>` and `<DISCORD_TOKEN>` with the actual values in your [Discord Developer Portal](https://discord.com/developers/applications/). | |
*/ | |
async function start() { | |
// Get the client ID and token from arguments or hard-coded values above. | |
const clientId = process.argv[2] || ClientId | |
const token = process.argv[3] || BotToken | |
if (!clientId || !token) { | |
throw 'β Please provide the Discord Client ID and Token.\n\nUsage:\nnode entry-fix.js <DISCORD_CLIENT_ID> <DISCORD_TOKEN>' | |
} | |
// Get the existing commands. | |
const commandsResponse = await request('/commands', { clientId, token }) | |
const commands = await commandsResponse.json() | |
loggerDebug('Existing commands:', commands) | |
if (!commandsResponse.ok) { | |
throw 'β Error fetching commands. ' + commands.message | |
} | |
// See if an entry command already exists. | |
let entryCommand = commands.find((command) => command.type === 4) | |
if (entryCommand) { | |
loggerDebug('Entry command:', entryCommand) | |
return 'π Entry command already exists. No need to fix.' | |
} | |
// Create the entry command if it doesn't exist. | |
const body = { | |
name: 'launch', | |
description: 'Launch an activity', | |
contexts: [0, 1, 2], | |
integration_types: [0, 1], | |
type: 4, | |
handler: 2 | |
} | |
const commandResponse = await request.post('/commands', { body, clientId, token }) | |
entryCommand = await commandResponse.json() | |
loggerDebug('Created command response:', entryCommand) | |
if (entryCommand.errors) { | |
throw 'β Error creating entry command.' | |
} | |
return 'π Successfully registered entry command.' | |
} | |
function loggerDebug(...message) { | |
if (Debug) { | |
console.log('π', ...message) | |
} | |
} | |
function request(path, options) { | |
const { body, clientId, method = 'GET', token } = options | |
return fetch('https://discord.com/api/v10/applications/' + clientId + path, { | |
body: body ? JSON.stringify(body) : undefined, | |
headers: { | |
Authorization: 'Bot ' + token, | |
'Content-Type': ['POST', 'PUT'].includes(method) ? 'application/json' : undefined | |
}, | |
method: method | |
}) | |
} | |
request.post = (path, options) => request(path, { ...options, method: 'POST' }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment