fix command permission handling and push discord to v10 (#640)

Co-authored-by: Max Isom <hi@maxisom.me>
This commit is contained in:
Kevin Kendzia 2022-05-14 02:44:14 +02:00 committed by GitHub
parent 1ef05aba9d
commit eb2885b206
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
38 changed files with 1214 additions and 644 deletions

View file

@ -1,58 +1,44 @@
import {Guild, Client} from 'discord.js';
import {Client, Guild} from 'discord.js';
import container from '../inversify.config.js';
import Command from '../commands';
import {TYPES} from '../types.js';
import Config from '../services/config.js';
import {prisma} from '../utils/db.js';
import {REST} from '@discordjs/rest';
import {Routes} from 'discord-api-types/v9';
import updatePermissionsForGuild from '../utils/update-permissions-for-guild.js';
import {Setting} from '@prisma/client';
import registerCommandsOnGuild from '../utils/register-commands-on-guild.js';
export default async (guild: Guild): Promise<void> => {
let invitedBy;
try {
const logs = await guild.fetchAuditLogs({type: 'BOT_ADD'});
invitedBy = logs.entries.find(entry => entry.target?.id === guild.client.user?.id)?.executor;
} catch {}
if (!invitedBy) {
console.warn(`Could not find user who invited Muse to ${guild.name} from the audit logs.`);
}
await prisma.setting.upsert({
export async function createGuildSettings(guild: Guild): Promise<Setting> {
return prisma.setting.upsert({
where: {
guildId: guild.id,
},
create: {
guildId: guild.id,
invitedByUserId: invitedBy?.id,
},
update: {
invitedByUserId: invitedBy?.id,
},
update: {},
});
}
export default async (guild: Guild): Promise<void> => {
await createGuildSettings(guild);
const config = container.get<Config>(TYPES.Config);
// Setup slash commands
if (!config.REGISTER_COMMANDS_ON_BOT) {
const token = container.get<Config>(TYPES.Config).DISCORD_TOKEN;
const client = container.get<Client>(TYPES.Client);
const rest = new REST({version: '9'}).setToken(token);
const rest = new REST({version: '10'}).setToken(config.DISCORD_TOKEN);
await rest.put(
Routes.applicationGuildCommands(client.user!.id, guild.id),
{body: container.getAll<Command>(TYPES.Command).map(command => command.slashCommand.toJSON())},
);
await registerCommandsOnGuild({
rest,
applicationId: client.user!.id,
guildId: guild.id,
commands: container.getAll<Command>(TYPES.Command).map(command => command.slashCommand),
});
}
await updatePermissionsForGuild(guild);
if (invitedBy) {
await invitedBy.send('👋 Hi! You just invited me to a server. I can\'t be used by your server members until you complete setup by running /config set-role in your server.');
} else {
const owner = await guild.fetchOwner();
await owner.send('👋 Hi! Someone (probably you) just invited me to a server you own. I can\'t be used by your server members until you complete setup by running /config set-role in your server.');
}
const owner = await guild.fetchOwner();
await owner.send('👋 Hi! Someone (probably you) just invited me to a server you own. I can\'t be used by your server members until you complete setup by running /config set-role in your server.');
};

View file

@ -1,10 +0,0 @@
import {Guild} from 'discord.js';
import updatePermissionsForGuild from '../utils/update-permissions-for-guild.js';
const handleGuildUpdate = async (oldGuild: Guild, newGuild: Guild) => {
if (oldGuild.ownerId !== newGuild.ownerId) {
await updatePermissionsForGuild(newGuild);
}
};
export default handleGuildUpdate;