forked from Galactic/galactic-bot
Merge branch 'slash-commands' of https://github.com/GalacticBot/New-GBot into slash-commands
This commit is contained in:
commit
98fad951fa
@ -32,7 +32,8 @@
|
|||||||
"developerGuilds": [
|
"developerGuilds": [
|
||||||
"264527028751958016",
|
"264527028751958016",
|
||||||
"207880433432657920",
|
"207880433432657920",
|
||||||
"992757341848080486"
|
"992757341848080486",
|
||||||
|
"1069272779100266598"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -13,7 +13,7 @@ class SlashCommandManager {
|
|||||||
|
|
||||||
this.rest = new REST({ version: '9' })
|
this.rest = new REST({ version: '9' })
|
||||||
.setToken(process.env.DISCORD_TOKEN);
|
.setToken(process.env.DISCORD_TOKEN);
|
||||||
|
|
||||||
this.hashPath = path.join(process.cwd(), '/commandHash.json');
|
this.hashPath = path.join(process.cwd(), '/commandHash.json');
|
||||||
this.hash = fs.existsSync(this.hashPath) ?
|
this.hash = fs.existsSync(this.hashPath) ?
|
||||||
JSON.parse(fs.readFileSync(this.hashPath, { encoding: 'utf-8' })) :
|
JSON.parse(fs.readFileSync(this.hashPath, { encoding: 'utf-8' })) :
|
||||||
@ -24,28 +24,34 @@ class SlashCommandManager {
|
|||||||
async _handleMessage({ commands, ...message }) {
|
async _handleMessage({ commands, ...message }) {
|
||||||
if (message.type === 'global') {
|
if (message.type === 'global') {
|
||||||
await this.global(commands, message.clientId);
|
await this.global(commands, message.clientId);
|
||||||
} else if(message.type === 'guild') {
|
} else if (message.type === 'guild') {
|
||||||
await this.guild(commands, { guilds: message.guilds, clientId: message.clientId });
|
await this.guild(commands, { guilds: message.guilds, clientId: message.clientId });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async guild(commands, { guilds = [], clientId }) {
|
async guild(commands, { guilds = [], clientId }) {
|
||||||
|
|
||||||
if (!guilds.length) guilds = this.client._options.discord.slashCommands.developerGuilds;
|
if (!guilds.length) guilds = this.client._options.discord.slashCommands.developerGuilds;
|
||||||
|
|
||||||
const cmdHash = hash(commands);
|
const cmdHash = hash(commands);
|
||||||
for (const guild of [...guilds]) {
|
for (const guild of [...guilds]) {
|
||||||
|
// Skip guild if unavailable
|
||||||
|
const res = await this.rest.get(Routes.guild(guild)).catch(() => { return null });
|
||||||
|
if (!res) {
|
||||||
|
guilds.splice(guilds.indexOf(guild), 1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
// Skip guild update if hash is already up to date
|
// Skip guild update if hash is already up to date
|
||||||
if (this.hash.guilds[guild] === cmdHash) guilds.splice(guilds.indexOf(guild), 1);
|
if (this.hash.guilds[guild] === cmdHash) guilds.splice(guilds.indexOf(guild), 1);
|
||||||
// else update hash
|
// else update hash
|
||||||
else this.hash.guilds[guild] = cmdHash;
|
else this.hash.guilds[guild] = cmdHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.client.logger.write('info', `Commands hash: ${cmdHash}, ${guilds.length} out of date`);
|
this.client.logger.write('info', `Commands hash: ${cmdHash}, ${guilds.length} out of date`);
|
||||||
if (!guilds.length) return;
|
if (!guilds.length) return;
|
||||||
const promises = [];
|
const promises = [];
|
||||||
//fs.writeFileSync(path.join(process.cwd(), 'commands.json'), JSON.stringify(commands));
|
//fs.writeFileSync(path.join(process.cwd(), 'commands.json'), JSON.stringify(commands));
|
||||||
for(const guild of guilds) {
|
for (const guild of guilds) {
|
||||||
promises.push(this.rest.put(
|
promises.push(this.rest.put(
|
||||||
Routes.applicationGuildCommands(clientId, guild),
|
Routes.applicationGuildCommands(clientId, guild),
|
||||||
{ body: commands }
|
{ body: commands }
|
||||||
@ -84,7 +90,7 @@ class SlashCommandManager {
|
|||||||
this.client.logger.error(`Failed commands:\n${str}`);
|
this.client.logger.error(`Failed commands:\n${str}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!result) return null;
|
if (!result) return null;
|
||||||
|
|
||||||
this.saveHash();
|
this.saveHash();
|
||||||
this.client.logger.debug(`Refreshed guild slash commands for guild${guilds.length === 1 ? '' : 's'}: ${guilds.join(' ')}`);
|
this.client.logger.debug(`Refreshed guild slash commands for guild${guilds.length === 1 ? '' : 's'}: ${guilds.join(' ')}`);
|
||||||
@ -96,7 +102,7 @@ class SlashCommandManager {
|
|||||||
|
|
||||||
const cmdHash = hash(commands);
|
const cmdHash = hash(commands);
|
||||||
const upToDate = this.hash.global === cmdHash;
|
const upToDate = this.hash.global === cmdHash;
|
||||||
this.client.logger.info(`Commands hash: ${cmdHash}, ${upToDate?'not ':''}updating`);
|
this.client.logger.info(`Commands hash: ${cmdHash}, ${upToDate ? 'not ' : ''}updating`);
|
||||||
if (upToDate) return;
|
if (upToDate) return;
|
||||||
|
|
||||||
this.hash.global = cmdHash;
|
this.hash.global = cmdHash;
|
||||||
@ -108,7 +114,7 @@ class SlashCommandManager {
|
|||||||
{ body: commands }
|
{ body: commands }
|
||||||
);
|
);
|
||||||
this.client.logger.debug('Finished global refresh for slash commands.');
|
this.client.logger.debug('Finished global refresh for slash commands.');
|
||||||
} catch(error) {
|
} catch (error) {
|
||||||
return this.client.logger.error(`Failed to refresh slash commands globally.\n${error.stack || error}`);
|
return this.client.logger.error(`Failed to refresh slash commands globally.\n${error.stack || error}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,11 +10,11 @@ class MongoDBProvider extends Provider {
|
|||||||
config
|
config
|
||||||
});
|
});
|
||||||
|
|
||||||
const { MONGODB_HOST, MONGODB_USERNAME, MONGODB_PASSWORD, MONGODB_DATABASE } = process.env;
|
const { MONGODB_HOST, MONGODB_USERNAME, MONGODB_PASSWORD, MONGODB_DATABASE, MONGODB_AUTH_SOURCE } = process.env;
|
||||||
this.database = MONGODB_DATABASE;
|
this.database = MONGODB_DATABASE;
|
||||||
// const { database } = this.config;
|
// const { database } = this.config;
|
||||||
const auth = MONGODB_USERNAME ? `${MONGODB_USERNAME}:${MONGODB_PASSWORD}@`:'';
|
const auth = MONGODB_USERNAME ? `${MONGODB_USERNAME}:${MONGODB_PASSWORD}@`:'';
|
||||||
this.URI = `mongodb://${auth}${MONGODB_HOST}/${this.database}?authSource=${this.database}`;
|
this.URI = `mongodb://${auth}${MONGODB_HOST}/${this.database}?authSource=${MONGODB_AUTH_SOURCE || this.database}`;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user