aboutsummaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
authorMax Isom <[email protected]>2022-01-29 11:20:40 -0500
committerMax Isom <[email protected]>2022-01-29 11:20:40 -0500
commit1621b2c2815f7458df19320361a4a5a41c9cf4d3 (patch)
tree47c7c1377243c5d91a95c0c7d9217ad3a3b9cfdb /src/commands
parent8e00726dc2c8179c7aa03f72e96544e78b4fb001 (diff)
downloadmuse-1621b2c2815f7458df19320361a4a5a41c9cf4d3.tar.xz
muse-1621b2c2815f7458df19320361a4a5a41c9cf4d3.zip
Add permissions system
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/config.ts103
-rw-r--r--src/commands/favorites.ts12
2 files changed, 111 insertions, 4 deletions
diff --git a/src/commands/config.ts b/src/commands/config.ts
new file mode 100644
index 0000000..65aa36b
--- /dev/null
+++ b/src/commands/config.ts
@@ -0,0 +1,103 @@
+import {SlashCommandBuilder} from '@discordjs/builders';
+import {CommandInteraction, MessageEmbed} from 'discord.js';
+import {injectable} from 'inversify';
+import {prisma} from '../utils/db.js';
+import updatePermissionsForGuild from '../utils/update-permissions-for-guild.js';
+import Command from './index.js';
+
+@injectable()
+export default class implements Command {
+ public readonly slashCommand = new SlashCommandBuilder()
+ .setName('config')
+ .setDescription('configure bot settings')
+ .addSubcommand(subcommand => subcommand
+ .setName('set-playlist-limit')
+ .setDescription('set the maximum number of tracks that can be added from a playlist')
+ .addIntegerOption(option => option
+ .setName('limit')
+ .setDescription('maximum number of tracks')
+ .setRequired(true)))
+ .addSubcommand(subcommand => subcommand
+ .setName('set-role')
+ .setDescription('set the role that is allowed to use the bot')
+ .addRoleOption(option => option
+ .setName('role')
+ .setDescription('allowed role')
+ .setRequired(true)))
+ .addSubcommand(subcommand => subcommand
+ .setName('get')
+ .setDescription('show all settings'));
+
+ async execute(interaction: CommandInteraction) {
+ switch (interaction.options.getSubcommand()) {
+ case 'set-playlist-limit': {
+ const limit = interaction.options.getInteger('limit')!;
+
+ if (limit < 1) {
+ throw new Error('invalid limit');
+ }
+
+ await prisma.setting.update({
+ where: {
+ guildId: interaction.guild!.id,
+ },
+ data: {
+ playlistLimit: limit,
+ },
+ });
+
+ await interaction.reply('👍 limit updated');
+
+ break;
+ }
+
+ case 'set-role': {
+ const role = interaction.options.getRole('role')!;
+
+ await prisma.setting.update({
+ where: {
+ guildId: interaction.guild!.id,
+ },
+ data: {
+ roleId: role.id,
+ },
+ });
+
+ await updatePermissionsForGuild(interaction.guild!);
+
+ await interaction.reply('👍 role updated');
+
+ break;
+ }
+
+ case 'get': {
+ const embed = new MessageEmbed().setTitle('Config');
+
+ const config = await prisma.setting.findUnique({where: {guildId: interaction.guild!.id}});
+
+ if (!config) {
+ throw new Error('no config found');
+ }
+
+ const settingsToShow = {
+ 'Playlist Limit': config.playlistLimit,
+ Role: config.roleId ? `<@&${config.roleId}>` : 'not set',
+ };
+
+ let description = '';
+ for (const [key, value] of Object.entries(settingsToShow)) {
+ description += `**${key}**: ${value}\n`;
+ }
+
+ embed.setDescription(description);
+
+ await interaction.reply({embeds: [embed]});
+
+ break;
+ }
+
+ default:
+ throw new Error('unknown subcommand');
+ }
+ }
+}
diff --git a/src/commands/favorites.ts b/src/commands/favorites.ts
index b15a5c9..98f1ba9 100644
--- a/src/commands/favorites.ts
+++ b/src/commands/favorites.ts
@@ -75,6 +75,7 @@ export default class implements Command {
}
async handleAutocompleteInteraction(interaction: AutocompleteInteraction) {
+ const subcommand = interaction.options.getSubcommand();
const query = interaction.options.getString('name')!.trim();
const favorites = await prisma.favoriteQuery.findMany({
@@ -83,13 +84,16 @@ export default class implements Command {
},
});
- const names = favorites.map(favorite => favorite.name);
+ let results = query === '' ? favorites : favorites.filter(f => f.name.startsWith(query));
- const results = query === '' ? names : names.filter(name => name.startsWith(query));
+ if (subcommand === 'remove') {
+ // Only show favorites that user is allowed to remove
+ results = interaction.member?.user.id === interaction.guild?.ownerId ? results : results.filter(r => r.authorId === interaction.member!.user.id);
+ }
await interaction.respond(results.map(r => ({
- name: r,
- value: r,
+ name: r.name,
+ value: r.name,
})));
}