From 85f031c33a7fb6fa11d7e7580558dd1ce79e4cd2 Mon Sep 17 00:00:00 2001 From: DeSqBlocki Date: Thu, 13 Feb 2025 21:04:53 +0100 Subject: [PATCH] added settings --- events/Twitch/Message.js | 10 ++++- events/Twitch/Shoutout.js | 18 +++++++- events/TwitchCommands/commands.js | 44 +++++++++++------- events/TwitchCommands/donate.js | 6 +-- events/TwitchCommands/mode.js | 8 ++-- events/TwitchCommands/positivity.js | 6 +-- events/TwitchCommands/settings.js | 69 +++++++++++++++++++++++++++++ events/TwitchCommands/shoutout.js | 23 ++++++++++ 8 files changed, 155 insertions(+), 29 deletions(-) create mode 100644 events/TwitchCommands/settings.js create mode 100644 events/TwitchCommands/shoutout.js diff --git a/events/Twitch/Message.js b/events/Twitch/Message.js index 20da3e1..6112e73 100644 --- a/events/Twitch/Message.js +++ b/events/Twitch/Message.js @@ -21,7 +21,7 @@ module.exports = { }) } } - + if (self) { return } if (knownBots.has(userstate.username)) { return } @@ -54,7 +54,13 @@ module.exports = { dClient.emit('Command/positivity', channel, userstate, message, self) break; case "commands": - dClient.emit('Command/commands', channel, userstate, message, self, args[1], args[2]) + dClient.emit('Command/commands', channel, userstate, message, self, args[1], args[2]) + break; + case "settings": + dClient.emit('Command/settings', channel, userstate, message, self, args[1], args[2]) + break; + case "so": + dClient.emit('Command/so', channel, userstate, message, self, args[1]) break; default: break; diff --git a/events/Twitch/Shoutout.js b/events/Twitch/Shoutout.js index 37addf8..dfe60f8 100644 --- a/events/Twitch/Shoutout.js +++ b/events/Twitch/Shoutout.js @@ -1,4 +1,4 @@ -const { tClient } = require("../.."); +const { tClient, mClient } = require("../.."); const { getChannelInformation } = require("../../functions"); // Event triggered by Custom Shoutout emitted in Messages.js @@ -9,7 +9,21 @@ module.exports = { name: 'Twitch/Shoutout', once: false, async execute(reason, channel, username) { - + // Check if shoutout is enabled + const db = mClient.db('settings') + const coll = db.collection(channel) + const setting = await coll.findOne({ name: 'shoutout' }) + + // create in disabled state if not exists + if (!setting) { + await coll.insertOne({ + name: 'shoutout', + enabled: false // default + }) + } + + if (!setting?.enabled && reason != 'command' ){ return } + // Function to perform shoutout (Nightbot or custom) async function doShoutouts(channel, user) { try { diff --git a/events/TwitchCommands/commands.js b/events/TwitchCommands/commands.js index 976d413..b000676 100644 --- a/events/TwitchCommands/commands.js +++ b/events/TwitchCommands/commands.js @@ -3,54 +3,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') + async execute(channel, userstate, message, self, state, name) { + const db = mClient.db('commands') const coll = db.collection(channel) + const defaultCommands = await coll.find({ default: true}).toArray() + var isDefault = false + defaultCommands.forEach((command) => { + if(command.default && command.name == name ){ + isDefault = true + } + }) + if (isDefault){ + return tClient.say(channel, `${name} can't be modified`) + } const command = await coll.findOne({ name: 'commands' }) - // create if not exists + // create in disabled state if not exists if (!command) { await coll.insertOne({ name: 'commands', - enabled: false // default + enabled: true, + default: true }) } // Default non-admin mode, give list of all enabled commands if (!name) { - const commands = await coll.find({ enabled: true }).toArray() + const commands = await coll.find({}).toArray() var commandList = [] commands.forEach((command) => { - if (command.enabled) { - commandList.push(command.name) + if(userstate.badges.moderator != 1 && userstate.badges.broadcaster != 1){ + commandList.push(` ${command.name}`) + } else { + commandList.push(` ${command.name}(${command.enabled?'✅':'❌'})`) } + }) if (commandList.length == 0) { - return await tClient.say(channel, `No commands enabled in this channel`) + return await tClient.say(channel, `No commands available in this channel`) } - console.log(commandList) - await tClient.say(channel, `Command List: ${commandList.toString()}`) + return 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': + case 'enable': state = true break; - case 'off': + case 'disable': state = false break; default: - break; + return tClient.say(channel, `invalid state, expecting 'enable' or 'disable'`) } // 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`) } @@ -63,6 +77,6 @@ module.exports = { upsert: true }) - await tClient.say(channel, `[${name}] has been set to [${state}]`) + return await tClient.say(channel, `[${name}] has been set to [${state}]`) } } \ No newline at end of file diff --git a/events/TwitchCommands/donate.js b/events/TwitchCommands/donate.js index 726cdb1..5a5c6c6 100644 --- a/events/TwitchCommands/donate.js +++ b/events/TwitchCommands/donate.js @@ -5,11 +5,11 @@ module.exports = { once: false, async execute(channel, userstate, message, self) { - const db = mClient.db('channels') + const db = mClient.db('commands') const coll = db.collection(channel) const command = await coll.findOne({ name: 'donate' }) - // create if not exists + // create in disabled state if not exists if (!command) { await coll.insertOne({ name: 'donate', @@ -17,7 +17,7 @@ module.exports = { }) } - if (!command.enabled){ return } + if (!command?.enabled){ return tClient.say(channel, `This command is currently disabled `)} async function sendDonationAd() { // Function to send static donate ad diff --git a/events/TwitchCommands/mode.js b/events/TwitchCommands/mode.js index 4a02d9e..7895e16 100644 --- a/events/TwitchCommands/mode.js +++ b/events/TwitchCommands/mode.js @@ -1,21 +1,21 @@ -const { mClient } = require('../..') +const { mClient, tClient } = require('../..') const { updateChatMode, getIDByName } = require('../../functions') module.exports = { name: 'Command/mode', once: false, async execute(channel, userstate, message, self, params) { - const db = mClient.db('channels') + const db = mClient.db('commands') const coll = db.collection(channel) const command = await coll.findOne({ name: 'mode' }) - // create if not exists + // create in disabled state if not exists if (!command) { await coll.insertOne({ name: 'mode', enabled: false // default }) } - if (!command.enabled){ return } + if (!command?.enabled){ return tClient.say(channel, `This command is currently disabled `) } if (userstate.badges.moderator != 1 && userstate.badges.broadcaster != 1) { return } let regex = /^(on|off)$/ diff --git a/events/TwitchCommands/positivity.js b/events/TwitchCommands/positivity.js index 8d0b9fd..d489822 100644 --- a/events/TwitchCommands/positivity.js +++ b/events/TwitchCommands/positivity.js @@ -3,11 +3,11 @@ module.exports = { name: 'Command/positivity', once: false, async execute(channel, userstate, message, self) { - const db = mClient.db('channels') + const db = mClient.db('commands') const coll = db.collection(channel) const command = await coll.findOne({ name: 'positivity' }) - // create if not exists + // create in disabled state if not exists if (!command) { await coll.insertOne({ name: 'positivity', @@ -15,7 +15,7 @@ module.exports = { }) } - if (!command.enabled){ return } + if (!command?.enabled){ return tClient.say(channel, `This command is currently disabled `) } async function sendPositivity() { // Function sends a random string of predetermined positivity, Donothon Top Donator Reward diff --git a/events/TwitchCommands/settings.js b/events/TwitchCommands/settings.js new file mode 100644 index 0000000..dfa0601 --- /dev/null +++ b/events/TwitchCommands/settings.js @@ -0,0 +1,69 @@ +const { mClient, tClient } = require("../.."); + +module.exports = { + name: 'Command/settings', + once: false, + async execute(channel, userstate, message, self, state, name) { + if (userstate.badges.moderator != 1 && userstate.badges.broadcaster != 1) { return } + const db = mClient.db('commands') + const coll = db.collection(channel) + const command = await coll.findOne({ name: 'settings' }) + + // create in disabled state if not exists + if (!command) { + await coll.insertOne({ + name: 'settings', + enabled: true, + default: true + }) + } + if (!command?.enabled) { return tClient.say(channel, `This command is currently disabled `) } + + const db2 = mClient.db('settings') + const coll2 = db2.collection(channel) + + // Default non-admin mode, give list of all enabled commands + if (!name) { + const settings = await coll2.find({}).toArray() + var settingsList = [] + console.log(settings) + settings.forEach((setting) => { + settingsList.push(` ${setting.name}(${setting.enabled?'✅':'❌'})`) + }) + if (settingsList.length == 0) { + return await tClient.say(channel, `No settings available in this channel`) + } + return await tClient.say(channel, `Settings List: ${settingsList.toString()}`) + } + + if (!state) { return } + + // Parameters are valid + switch (state) { + case 'enable': + state = true + break; + case 'disable': + state = false + break; + default: + return tClient.say(channel, `invalid state, expecting 'enable' or 'disable'`) + } + // command is valid + + const findSetting = await coll2.findOne({ name: name }) + if (!findSetting) { return await tClient.say(channel, `Setting [${name}] not found.`) } + + + await coll2.findOneAndUpdate({ + name: name + }, { + $set: { enabled: state } + }, { + upsert: true + }) + + return await tClient.say(channel, `[${name}] has been set to [${state}]`) + + } +} \ No newline at end of file diff --git a/events/TwitchCommands/shoutout.js b/events/TwitchCommands/shoutout.js new file mode 100644 index 0000000..fc638e5 --- /dev/null +++ b/events/TwitchCommands/shoutout.js @@ -0,0 +1,23 @@ +const { mClient, dClient, tClient } = require('../..') +const { updateChatMode, getIDByName } = require('../../functions') +module.exports = { + name: 'Command/so', + once: false, + async execute(channel, userstate, message, self, name) { + if (userstate.badges.moderator != 1 && userstate.badges.broadcaster != 1) { return } + const db = mClient.db('commands') + const coll = db.collection(channel) + const command = await coll.findOne({ name: 'so' }) + + // create in disabled state if not exists + if (!command) { + await coll.insertOne({ + name: 'so', + enabled: false // default + }) + } + if (!command?.enabled) { return tClient.say(channel, `This command is currently disabled `) } + + dClient.emit('Twitch/Shoutout', 'command', channel, name) + } +} \ No newline at end of file