separated commands from message event and added state control to them

This commit is contained in:
DeSqBlocki
2025-02-13 19:45:13 +01:00
parent fe0c937fb2
commit 5b07182020
6 changed files with 217 additions and 79 deletions
+68
View File
@@ -0,0 +1,68 @@
const { mClient, tClient } = require("../..");
module.exports = {
name: 'Command/commands',
once: false,
async execute(channel, userstate, message, self, name, state) {
const db = mClient.db('channels')
const coll = db.collection(channel)
const command = await coll.findOne({ name: 'commands' })
// create if not exists
if (!command) {
await coll.insertOne({
name: 'commands',
enabled: false // default
})
}
// Default non-admin mode, give list of all enabled commands
if (!name) {
const commands = await coll.find({ enabled: true }).toArray()
var commandList = []
commands.forEach((command) => {
if (command.enabled) {
commandList.push(command.name)
}
})
if (commandList.length == 0) {
return await tClient.say(channel, `No commands enabled in this channel`)
}
console.log(commandList)
await tClient.say(channel, `Command List: ${commandList.toString()}`)
}
if (userstate.badges.moderator != 1 && userstate.badges.broadcaster != 1) { return }
if (!state) { return }
// Admin Mode & Additional Parameters were given
// Parameters are valid
switch (state) {
case 'on':
state = true
break;
case 'off':
state = false
break;
default:
break;
}
// command is valid
const findCommand = await coll.findOne({ name: name })
if (!findCommand) { return await tClient.say(channel, `Command [${name}] not found. Try using it first if it exists`) }
await coll.findOneAndUpdate({
name: name
}, {
$set: { enabled: state }
}, {
upsert: true
})
await tClient.say(channel, `[${name}] has been set to [${state}]`)
}
}