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:
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 { MessageStyle } from './messageStyle.js'
|
||||
import { MessageStream } from './messageStream.js'
|
||||
import { Disable } from './disable.js'
|
||||
import { Shutoff } from './shutoff.js'
|
||||
|
||||
export default [
|
||||
ThreadCreate,
|
||||
MessageStyle,
|
||||
MessageStream
|
||||
MessageStream,
|
||||
Disable,
|
||||
Shutoff
|
||||
] 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 {
|
||||
const config: Configuration = await new Promise((resolve, reject) => {
|
||||
getConfig('config.json', (config) => {
|
||||
// check if config.json exists
|
||||
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
|
||||
}
|
||||
resolve(config)
|
||||
@@ -55,6 +62,6 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
|
||||
} catch (error: any) {
|
||||
msgHist.pop() // remove message because of failure
|
||||
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'),
|
||||
guildId: getEnvVar('GUILD_ID'),
|
||||
ipAddress: getEnvVar('OLLAMA_IP'),
|
||||
portAddress: getEnvVar('OLLAMA_PORT')
|
||||
portAddress: getEnvVar('OLLAMA_PORT'),
|
||||
superUser: getEnvVar('ADMINS')
|
||||
} as const // readonly keys
|
||||
|
||||
export default Keys
|
||||
@@ -4,7 +4,8 @@ export interface Configuration {
|
||||
readonly name: string
|
||||
options: {
|
||||
'message-stream'?: boolean,
|
||||
'message-style'?: boolean
|
||||
'message-style'?: boolean,
|
||||
'toggle-chat'?: boolean
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user