Skip to content

Commit

Permalink
fix: register global commands manually
Browse files Browse the repository at this point in the history
this avoids requiring the application.commands scope (and therefore
 updating all permissions of the main uncle bot instance), while also
 only requiring a single request on launch.
  • Loading branch information
bscotty committed Sep 2, 2022
1 parent 1a2a600 commit 27c9c7c
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const structureDamage = require('./util/structure-damage')
const stressDamage = require('./util/stress-damage')
require('dotenv').config()
const { Util } = require("discord.js")
const { Routes } = require('discord-api-types/v9');

/*
/data/index.js is the data cleaner/importer. the result of /data/ is a data object.
Expand Down Expand Up @@ -216,13 +217,23 @@ client.registry

client.login(process.env.TOKEN)
.then(async () => {
client.guilds.cache.map(async (guild) => {
console.log(`registering commands to guild ${guild.id}`)
await client.registry.registerSlashInGuild(guild)
})
await registerCommandsToAllGuilds()
})

client.on("guildCreate", async (guild) => {
console.log(`Joining guild ${guild.id}`)
await client.registry.registerSlashInGuild(guild)
})
})
// this is a hack, liable to break. it is based on the implementation of client.registry.registerSlashGlobally(),
// but it explicitly sets dm_permission for slash commands to false, which commando does not support doing.
async function registerCommandsToAllGuilds() {
const commands = client.registry._prepareCommandsForSlash().map((command) => {
// slash commands are type 1
if (command.type === 1) {
return { ...command, dm_permission: false }
} else {
return command
}
})

await client.registry.rest.put(
Routes.applicationCommands(client.user.id),
{ body: commands }
);
}

0 comments on commit 27c9c7c

Please sign in to comment.