Migrate to slash commands (#431)

Co-authored-by: Federico fuji97 Rapetti <fuji1097@gmail.com>
This commit is contained in:
Max Isom 2022-02-05 16:16:17 -06:00 committed by GitHub
parent e883275d83
commit 56a469a999
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
51 changed files with 1270 additions and 1294 deletions

View file

@ -1,10 +1,11 @@
import {Guild, TextChannel, Message, MessageReaction, User} from 'discord.js';
import emoji from 'node-emoji';
import pEvent from 'p-event';
import {chunk} from '../utils/arrays.js';
import {Guild, Client} 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';
const DEFAULT_PREFIX = '!';
import {REST} from '@discordjs/rest';
import {Routes} from 'discord-api-types/v9';
export default async (guild: Guild): Promise<void> => {
await prisma.setting.upsert({
@ -13,88 +14,26 @@ export default async (guild: Guild): Promise<void> => {
},
create: {
guildId: guild.id,
prefix: DEFAULT_PREFIX,
},
update: {
prefix: DEFAULT_PREFIX,
},
update: {},
});
const owner = await guild.client.users.fetch(guild.ownerId);
const config = container.get<Config>(TYPES.Config);
let firstStep = '👋 Hi!\n';
firstStep += 'I just need to ask a few questions before you start listening to music.\n\n';
firstStep += 'First, what channel should I listen to for music commands?\n\n';
// 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 firstStepMsg = await owner.send(firstStep);
const rest = new REST({version: '9'}).setToken(token);
// Show emoji selector
interface EmojiChannel {
name: string;
id: string;
emoji: string;
await rest.put(
Routes.applicationGuildCommands(client.user!.id, guild.id),
{body: container.getAll<Command>(TYPES.Command).map(command => command.slashCommand.toJSON())},
);
}
const emojiChannels: EmojiChannel[] = [];
const owner = await guild.fetchOwner();
for (const [channelId, channel] of guild.channels.cache) {
if (channel.type === 'GUILD_TEXT') {
emojiChannels.push({
name: channel.name,
id: channelId,
emoji: emoji.random().emoji,
});
}
}
const sentMessageIds: string[] = [];
chunk(emojiChannels, 10).map(async chunk => {
let str = '';
for (const channel of chunk) {
str += `${channel.emoji}: #${channel.name}\n`;
}
const msg = await owner.send(str);
sentMessageIds.push(msg.id);
await Promise.all(chunk.map(async channel => msg.react(channel.emoji)));
});
// Wait for response from user
const [choice] = await pEvent(guild.client, 'messageReactionAdd', {
multiArgs: true,
filter: ([reaction, user]: [MessageReaction, User]) => sentMessageIds.includes(reaction.message.id) && user.id === owner.id,
});
const chosenChannel = emojiChannels.find(e => e.emoji === (choice as unknown as MessageReaction).emoji.name)!;
// Second setup step (get prefix)
let secondStep = `👍 Cool, I'll listen to **#${chosenChannel.name}** \n\n`;
secondStep += 'Last question: what character should I use for a prefix? Type a single character and hit enter.';
await owner.send(secondStep);
const prefixResponses = await firstStepMsg.channel.awaitMessages({filter: (r: Message) => r.content.length === 1, max: 1});
const prefixCharacter = prefixResponses.first()!.content;
// Save settings
await prisma.setting.update({
where: {
guildId: guild.id,
},
data: {
channel: chosenChannel.id,
prefix: prefixCharacter,
},
});
// Send welcome
const boundChannel = guild.client.channels.cache.get(chosenChannel.id) as TextChannel;
await boundChannel.send(`hey <@${owner.id}> try \`${prefixCharacter}play https://www.youtube.com/watch?v=dQw4w9WgXcQ\``);
await firstStepMsg.channel.send(`Sounds good. Check out **#${chosenChannel.name}** to get started.`);
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

@ -0,0 +1,10 @@
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;