aboutsummaryrefslogtreecommitdiff
path: root/src/events
diff options
context:
space:
mode:
authorKevin Kendzia <[email protected]>2022-05-14 02:44:14 +0200
committerGitHub <[email protected]>2022-05-13 19:44:14 -0500
commiteb2885b2061708415e46929d21bc5991724ce441 (patch)
treee77496a2d2fdd249cc505ca1e06d7b17cce957ac /src/events
parent1ef05aba9d2e692ef365721f725be2d2a4e464d9 (diff)
downloadmuse-eb2885b2061708415e46929d21bc5991724ce441.tar.xz
muse-eb2885b2061708415e46929d21bc5991724ce441.zip
fix command permission handling and push discord to v10 (#640)
Co-authored-by: Max Isom <[email protected]>
Diffstat (limited to 'src/events')
-rw-r--r--src/events/guild-create.ts52
-rw-r--r--src/events/guild-update.ts10
2 files changed, 19 insertions, 43 deletions
diff --git a/src/events/guild-create.ts b/src/events/guild-create.ts
index d71acda..f509e45 100644
--- a/src/events/guild-create.ts
+++ b/src/events/guild-create.ts
@@ -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.');
};
diff --git a/src/events/guild-update.ts b/src/events/guild-update.ts
deleted file mode 100644
index 837bbbe..0000000
--- a/src/events/guild-update.ts
+++ /dev/null
@@ -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;