From b5194fa6452ea2474c942caed63433bf19c0fee9 Mon Sep 17 00:00:00 2001 From: Kevin Dang <77701718+kevinthedang@users.noreply.github.com> Date: Thu, 25 Apr 2024 11:26:22 -0700 Subject: [PATCH] Discord Administrator Role Permissions (#54) * add: admin check for disable * update: shutoff uses memberPerms now * rm: superUser env variable * update: version increment * rm: admin env in docker and workflow --- .env.sample | 3 --- .github/workflows/build-test.yml | 2 -- docker-compose.yml | 3 +-- package-lock.json | 4 ++-- package.json | 2 +- src/commands/disable.ts | 11 ++++++++++- src/commands/shutoff.ts | 15 ++++++--------- src/keys.ts | 1 - 8 files changed, 20 insertions(+), 21 deletions(-) diff --git a/.env.sample b/.env.sample index 33e8e33..418fa97 100644 --- a/.env.sample +++ b/.env.sample @@ -22,6 +22,3 @@ DISCORD_IP = IP_ADDRESS # subnet address, ex. 172.18.0.0 as we use /16. SUBNET_ADDRESS = ADDRESS - -# list of admins to handle admin commands for the bot, use single quotes -ADMINS=['username1', 'username2', 'username3', ...] \ No newline at end of file diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 03ae91e..0e86add 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -37,7 +37,6 @@ jobs: echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env - echo ADMINS = ${{ secrets.ADMINS }} >> .env # set -e ensures if nohup fails, this section fails - name: Startup Discord Bot Client @@ -68,7 +67,6 @@ jobs: echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env - echo ADMINS = ${{ secrets.ADMINS }} >> .env - name: Setup Docker Network and Images run: | diff --git a/docker-compose.yml b/docker-compose.yml index 752cd2a..17db434 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,7 +8,7 @@ services: build: ./ # find docker file in designated path container_name: discord restart: always # rebuild container always - image: discord/bot:0.4.2 + image: discord/bot:0.4.3 environment: CLIENT_TOKEN: ${CLIENT_TOKEN} GUILD_ID: ${GUILD_ID} @@ -17,7 +17,6 @@ services: CLIENT_UID: ${CLIENT_UID} OLLAMA_IP: ${OLLAMA_IP} OLLAMA_PORT: ${OLLAMA_PORT} - ADMINS: ${ADMINS} networks: ollama-net: ipv4_address: ${DISCORD_IP} diff --git a/package-lock.json b/package-lock.json index d3744f4..150fdd1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "discord-ollama", - "version": "0.4.0", + "version": "0.4.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "discord-ollama", - "version": "0.4.0", + "version": "0.4.3", "license": "ISC", "dependencies": { "discord.js": "^14.14.1", diff --git a/package.json b/package.json index 28a432a..5b2e3d9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discord-ollama", - "version": "0.4.2", + "version": "0.4.3", "description": "Ollama Integration into discord", "main": "build/index.js", "exports": "./build/index.js", diff --git a/src/commands/disable.ts b/src/commands/disable.ts index f26440c..70d5c47 100644 --- a/src/commands/disable.ts +++ b/src/commands/disable.ts @@ -4,7 +4,7 @@ import { openFile } from '../utils/jsonHandler.js' export const Disable: SlashCommand = { name: 'toggle-chat', - description: 'toggle all chat features, slash commands will still work.', + description: 'toggle all chat features, Adminstrator Only.', // set available user options to pass to the command options: [ @@ -22,6 +22,15 @@ export const Disable: SlashCommand = { const channel = await client.channels.fetch(interaction.channelId) if (!channel || channel.type !== ChannelType.GuildText) return + // check if runner is an admin + if (!interaction.memberPermissions?.has('Administrator')) { + interaction.reply({ + content: `${interaction.commandName} is an Administrator Command.\n\nYou, ${interaction.member?.user.username}, are not an Administrator in this server.\nPlease contact an admin to use this command.`, + ephemeral: true + }) + return + } + // set state of bot chat features openFile('config.json', interaction.commandName, interaction.options.get('enabled')?.value) diff --git a/src/commands/shutoff.ts b/src/commands/shutoff.ts index e954a78..b3ab5f1 100644 --- a/src/commands/shutoff.ts +++ b/src/commands/shutoff.ts @@ -1,10 +1,9 @@ import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js' import { SlashCommand } from '../utils/commands.js' -import Keys from '../keys.js' export const Shutoff: SlashCommand = { name: 'shutoff', - description: 'shutdown the bot. You will need to manually bring it online again.', + description: 'shutdown the bot. You will need to manually bring it online again. Administrator Only.', // set available user options to pass to the command options: [ @@ -25,24 +24,22 @@ export const Shutoff: SlashCommand = { // log this, this will probably be improtant for logging who did this console.log(`User -> ${interaction.user.tag} attempting to shutdown ${client.user!!.tag}`) - // create list of superUsers based on string parse - const superUsers: string[] = JSON.parse(Keys.superUser.replace(/'/g, '"')) - // check if admin or false on shutdown - if (interaction.user.tag !in superUsers) { + if (!interaction.memberPermissions?.has('Administrator')) { interaction.reply({ - content: `Shutdown failed:\n\n${interaction.user.tag}, You do not have permission to shutoff **${client.user?.tag}**.`, + content: `**Shutdown Aborted:**\n\n${interaction.user.tag}, You do not have permission to shutoff **${client.user?.tag}**.`, ephemeral: true }) return // stop from shutting down } else if (!interaction.options.get('are-you-sure')?.value) { interaction.reply({ - content: `Shutdown failed:\n\n${interaction.user.tag}, You didn't want to shutoff **${client.user?.tag}**.`, + content: `**Shutdown Aborted:**\n\n${interaction.user.tag}, You didn't want to shutoff **${client.user?.tag}**.`, ephemeral: true }) - return + return // chickened out } + // Shutoff cleared, do it interaction.reply({ content: `${client.user?.tag} is ${interaction.options.get('are-you-sure')?.value ? "shutting down now." : "not shutting down." }`, ephemeral: true diff --git a/src/keys.ts b/src/keys.ts index 3c2a6d4..0c82e36 100644 --- a/src/keys.ts +++ b/src/keys.ts @@ -8,7 +8,6 @@ export const Keys = { guildId: getEnvVar('GUILD_ID'), ipAddress: getEnvVar('OLLAMA_IP'), portAddress: getEnvVar('OLLAMA_PORT'), - superUser: getEnvVar('ADMINS') } as const // readonly keys export default Keys \ No newline at end of file