Shutoff Bot Command (#30)
* add: disable chat command * update: workflow name * add: shutoff using admin env list * update: sample env for admins * fix: shutdown booleans * update: version increment
This commit is contained in:
@@ -21,4 +21,7 @@ OLLAMA_PORT = PORT
|
|||||||
DISCORD_IP = IP_ADDRESS
|
DISCORD_IP = IP_ADDRESS
|
||||||
|
|
||||||
# subnet address, ex. 172.18.0.0 as we use /16.
|
# subnet address, ex. 172.18.0.0 as we use /16.
|
||||||
SUBNET_ADDRESS = ADDRESS
|
SUBNET_ADDRESS = ADDRESS
|
||||||
|
|
||||||
|
# list of admins to handle admin commands for the bot, use single quotes
|
||||||
|
ADMINS=['username1', 'username2', 'username3', ...]
|
||||||
3
.github/workflows/build-test.yml
vendored
3
.github/workflows/build-test.yml
vendored
@@ -1,4 +1,4 @@
|
|||||||
name: Test Discord-Ollama Builds
|
name: Builds
|
||||||
run-name: Validate Node and Docker Builds
|
run-name: Validate Node and Docker Builds
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
@@ -74,7 +74,6 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
(docker images | grep -q 'discord/bot' && docker images | grep -qE 'ollama/ollama') || exit 1
|
(docker images | grep -q 'discord/bot' && docker images | grep -qE 'ollama/ollama') || exit 1
|
||||||
|
|
||||||
|
|
||||||
- name: Check Containers Exist
|
- name: Check Containers Exist
|
||||||
run: |
|
run: |
|
||||||
(docker ps | grep -q 'ollama' && docker ps | grep -q 'discord') || exit 1
|
(docker ps | grep -q 'ollama' && docker ps | grep -q 'discord') || exit 1
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ services:
|
|||||||
build: ./ # find docker file in designated path
|
build: ./ # find docker file in designated path
|
||||||
container_name: discord
|
container_name: discord
|
||||||
restart: always # rebuild container always
|
restart: always # rebuild container always
|
||||||
image: discord/bot:0.2.0
|
image: discord/bot:0.3.4
|
||||||
environment:
|
environment:
|
||||||
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
||||||
GUILD_ID: ${GUILD_ID}
|
GUILD_ID: ${GUILD_ID}
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.3.3",
|
"version": "0.3.4",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.3.3",
|
"version": "0.3.4",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.6.2",
|
"axios": "^1.6.2",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.3.3",
|
"version": "0.3.4",
|
||||||
"description": "Ollama Integration into discord",
|
"description": "Ollama Integration into discord",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
"exports": "./build/index.js",
|
"exports": "./build/index.js",
|
||||||
|
|||||||
33
src/commands/disable.ts
Normal file
33
src/commands/disable.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||||
|
import { SlashCommand } from '../utils/commands.js'
|
||||||
|
import { openFile } from '../utils/jsonHandler.js'
|
||||||
|
|
||||||
|
export const Disable: SlashCommand = {
|
||||||
|
name: 'toggle-chat',
|
||||||
|
description: 'toggle all chat features, slash commands will still work.',
|
||||||
|
|
||||||
|
// set available user options to pass to the command
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: 'enabled',
|
||||||
|
description: 'true = enabled, false = disabled',
|
||||||
|
type: ApplicationCommandOptionType.Boolean,
|
||||||
|
required: true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
|
||||||
|
// Query for message information and set the style
|
||||||
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
|
// fetch channel and message
|
||||||
|
const channel = await client.channels.fetch(interaction.channelId)
|
||||||
|
if (!channel || channel.type !== ChannelType.GuildText) return
|
||||||
|
|
||||||
|
// set state of bot chat features
|
||||||
|
openFile('config.json', interaction.commandName, interaction.options.get('enabled')?.value)
|
||||||
|
|
||||||
|
interaction.reply({
|
||||||
|
content: `Chat features has been \`${interaction.options.get('enabled')?.value ? "enabled" : "disabled" }\``,
|
||||||
|
ephemeral: true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,9 +2,13 @@ import { SlashCommand } from '../utils/commands.js'
|
|||||||
import { ThreadCreate } from './threadCreate.js'
|
import { ThreadCreate } from './threadCreate.js'
|
||||||
import { MessageStyle } from './messageStyle.js'
|
import { MessageStyle } from './messageStyle.js'
|
||||||
import { MessageStream } from './messageStream.js'
|
import { MessageStream } from './messageStream.js'
|
||||||
|
import { Disable } from './disable.js'
|
||||||
|
import { Shutoff } from './shutoff.js'
|
||||||
|
|
||||||
export default [
|
export default [
|
||||||
ThreadCreate,
|
ThreadCreate,
|
||||||
MessageStyle,
|
MessageStyle,
|
||||||
MessageStream
|
MessageStream,
|
||||||
|
Disable,
|
||||||
|
Shutoff
|
||||||
] as SlashCommand[]
|
] as SlashCommand[]
|
||||||
48
src/commands/shutoff.ts
Normal file
48
src/commands/shutoff.ts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
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.',
|
||||||
|
|
||||||
|
// set available user options to pass to the command
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: 'are-you-sure',
|
||||||
|
description: 'true = yes, false = I\'m scared',
|
||||||
|
type: ApplicationCommandOptionType.Boolean,
|
||||||
|
required: true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
|
||||||
|
// Query for message information and set the style
|
||||||
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
|
// fetch channel and message
|
||||||
|
const channel = await client.channels.fetch(interaction.channelId)
|
||||||
|
if (!channel || channel.type !== ChannelType.GuildText) return
|
||||||
|
|
||||||
|
// 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 || !(!interaction.options.get('are-you-sure')?.value && interaction.user.tag in superUsers)) {
|
||||||
|
interaction.reply({
|
||||||
|
content: `Shutdown failed:\n\n${interaction.user.tag}, You do not have permission to shutoff **${client.user?.tag}**, otherwise, you just didn't want to.`,
|
||||||
|
ephemeral: true
|
||||||
|
})
|
||||||
|
return // stop from shutting down
|
||||||
|
}
|
||||||
|
|
||||||
|
interaction.reply({
|
||||||
|
content: `${client.user?.tag} is ${interaction.options.get('are-you-sure')?.value ? "shutting down now." : "not shutting down." }`,
|
||||||
|
ephemeral: true
|
||||||
|
})
|
||||||
|
|
||||||
|
// clean up client instance and stop
|
||||||
|
client.destroy()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,8 +28,15 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
|
|||||||
try {
|
try {
|
||||||
const config: Configuration = await new Promise((resolve, reject) => {
|
const config: Configuration = await new Promise((resolve, reject) => {
|
||||||
getConfig('config.json', (config) => {
|
getConfig('config.json', (config) => {
|
||||||
|
// check if config.json exists
|
||||||
if (config === undefined) {
|
if (config === undefined) {
|
||||||
reject(new Error('No Configuration is set up.'))
|
reject(new Error('No Configuration is set up.\n\nCreating \`config.json\` with \`message-style\` set as \`true\` for embedded messages.\nPlease try chatting again.'))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if chat is disabled
|
||||||
|
if(!config.options['toggle-chat']) {
|
||||||
|
reject(new Error('Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).'))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resolve(config)
|
resolve(config)
|
||||||
@@ -55,6 +62,6 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
|
|||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
msgHist.pop() // remove message because of failure
|
msgHist.pop() // remove message because of failure
|
||||||
openFile('config.json', 'message-style', true)
|
openFile('config.json', 'message-style', true)
|
||||||
message.reply(`**Response generation failed.**\n\n**Reason:** *${error.message}*\n\nCreating \`config.json\` with \`message-style\` set as \`true\` for embedded messages.\nPlease try chatting again.`)
|
message.reply(`**Response generation failed.**\n\n**Reason:** *${error.message}*`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -7,7 +7,8 @@ export const Keys = {
|
|||||||
clientUid: getEnvVar('CLIENT_UID'),
|
clientUid: getEnvVar('CLIENT_UID'),
|
||||||
guildId: getEnvVar('GUILD_ID'),
|
guildId: getEnvVar('GUILD_ID'),
|
||||||
ipAddress: getEnvVar('OLLAMA_IP'),
|
ipAddress: getEnvVar('OLLAMA_IP'),
|
||||||
portAddress: getEnvVar('OLLAMA_PORT')
|
portAddress: getEnvVar('OLLAMA_PORT'),
|
||||||
|
superUser: getEnvVar('ADMINS')
|
||||||
} as const // readonly keys
|
} as const // readonly keys
|
||||||
|
|
||||||
export default Keys
|
export default Keys
|
||||||
@@ -4,7 +4,8 @@ export interface Configuration {
|
|||||||
readonly name: string
|
readonly name: string
|
||||||
options: {
|
options: {
|
||||||
'message-stream'?: boolean,
|
'message-stream'?: boolean,
|
||||||
'message-style'?: boolean
|
'message-style'?: boolean,
|
||||||
|
'toggle-chat'?: boolean
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user