From b30fdc9e7fe83ac792bccf502468d1c018cd3cd8 Mon Sep 17 00:00:00 2001 From: ION606 Date: Fri, 30 Sep 2022 22:21:44 -0400 Subject: [PATCH] Added presence-changing commands to make dynamic bot moddification easier --- commands/dev only/setPresence.js | 84 ++++++++++++++++++++++++++++++++ commands/games/game.js | 4 +- main.js | 25 ++++++++-- registerCommands.js | 34 +++++++++++++ 4 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 commands/dev only/setPresence.js diff --git a/commands/dev only/setPresence.js b/commands/dev only/setPresence.js new file mode 100644 index 0000000..47cb299 --- /dev/null +++ b/commands/dev only/setPresence.js @@ -0,0 +1,84 @@ +const Discord = require('discord.js') +const axios = require('axios') +const cheerio = require('cheerio'); +const { URL } = require("url"); + +function isValidUrl(s) { + try { + new URL(s); + return true; + } catch (err) { + return false; + } +}; + +function handleStreamInp(bot, url, customTitle) { + return new Promise((resolve, reject) => { + if (!isValidUrl(url)) { + return reject(false); + } + + axios.get(url).then(async response => { + var title; + + if (customTitle) { + title = customTitle; + } else { + const html = response.data; + const $ = cheerio.load(html); + title = $('meta[name="description"]').attr("content"); + } + + bot.user.setActivity({name: title, type: "STREAMING", url: url}); + + resolve(true); + }); + }); +} + +//Have this only visible to you. +/** + * @param {Discord.Client} bot + * @param {Discord.Interaction} interaction + */ +async function setPresence(bot, interaction) { + const command = interaction.options.data[0]; + + if (command.name == "setpresence") { + const txt = command.options.filter((arg) => { return(arg.name == 'pres_text'); })[0].value; + const type = command.options.filter((arg) => { return(arg.name == 'type'); })[0].value; + + var sep = " "; + if (type == "LISTENING" || type == "WATCHING") { + sep = " to "; + } else if (type == "COMPETING") { + sep = " in "; + } + + //Check if it's me + if (interaction.user.id == bot.guilds.cache.get(bot.home_server).ownerId) { + if (type == "STREAMING") { + const t = (command.options.length > 2) ? command.options.filter((arg) => { return(arg.name == "display_name"); })[0].value : null; + + handleStreamInp(bot, txt, t, interaction).then(() => { + interaction.reply(`Set bot presence to _${type + sep + txt}_`); + }).catch(() => { + interaction.reply("Invalid URL").catch((err) => { + interaction.channel.send("Invalid URL"); + }); + }); + } else { + bot.user.setActivity(txt, { type: type }); + interaction.reply(`Set bot presence to _${type + sep + txt}_`); + } + } + } else if (command.name == "setactivity") { + const stat = command.options[0]; + + bot.user.setStatus(stat.value); + interaction.reply(`Set bot status to ${stat.value}`); + } +} + + +module.exports = { setPresence } \ No newline at end of file diff --git a/commands/games/game.js b/commands/games/game.js index 76a0dfe..01b17a9 100644 --- a/commands/games/game.js +++ b/commands/games/game.js @@ -1,5 +1,7 @@ // // @ts-check //Disabled +// Maybe have the interaction type be "user" https://canary.discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-types + const { MongoClient, ServerApiVersion } = require('mongodb'); let ecoimport = require("../db/econ.js"); @@ -20,7 +22,7 @@ const { chooseClass, presentClasses } = require('./game_classes.js'); //Has a list of all games (used to change player state) const allGames = ['battle', 'Tic Tac Toe']; -// const { NULL } = require('mysql/lib/protocol/constants/types'); + //#region functions (NOT GAME SPECIFIC) diff --git a/main.js b/main.js index 27d3701..e479309 100644 --- a/main.js +++ b/main.js @@ -15,6 +15,7 @@ const { devCheck } = require('./commands/dev only/devcheck.js'); const { moderation_handler } = require('./commands/admin/moderation.js'); const { registerCommands } = require('./registerCommands.js'); const { backupLists, loadBotBackups } = require('./commands/admin/backupBot.js'); +const { setPresence } = require('./commands/dev only/setPresence.js'); const { exit } = require('process'); //#endregion @@ -184,9 +185,15 @@ bot.on('ready', async () => { items = [...itemstemp]; }); - //Srt status and Activity (idle and listening to !help) - bot.user.setActivity(`${bot.prefix}help`, { type: "LISTENING" }); - // bot.user.setStatus('idle'); + + bot.user.setStatus('idle'); + //Why doesn't this work? + // bot.user.setPresence({ + // status: 'online', activity: { + // name: "It's only logical!", + // type: "LISTENING" + // } + // }); }); //Note the xp numbers are a little wonky on levels 6, 8 and 13 (why though?) @@ -216,14 +223,22 @@ bot.on('ready', async () => { //Button Section bot.on('interactionCreate', async interaction => { const { commandName } = interaction; - bot.lockedChannels.set(interaction.guildId, ["NUMBERS HERE"]); // console.log(bot.lockedChannels); //Slash commands if (interaction.isApplicationCommand()) { const logable = ['kick', 'ban', 'unban', 'mute', 'unmute', 'timeout']; const econList = ["buy", 'shop', 'work', 'rank', 'inventory', 'balance', 'sell']; + const adminList = ["setpresence", "setactivity"]; - if (logable.includes(commandName)) { + if (commandName == "admin" && adminList.includes(interaction.options.data[0].name)) { + if (interaction.user.id == bot.guilds.cache.get(bot.home_server).ownerId) { + setPresence(bot, interaction); + } else { + return interaction.reply({ content: "HAHAHAHAHAHAHAHAHAHAHA\n\nno.", ephemeral: true }).catch((err) => { + interaction.channel.send({ content: "HAHAHAHAHAHAHAHAHAHAHA\n\nno.", ephemeral: true }); + }); + } + } else if (logable.includes(commandName)) { moderation_handler(bot, interaction, commandName); } else if (econList.includes(commandName)) { bot.commands.get('econ').execute(bot, interaction, Discord, mongouri, items, xp_collection); diff --git a/registerCommands.js b/registerCommands.js index 3901b46..b8d196b 100644 --- a/registerCommands.js +++ b/registerCommands.js @@ -76,6 +76,40 @@ function registerCommands(bot) { // comm.setDefaultMemberPermissions(Discord.PermissionFlagsBits.KickMembers | Discord.PermissionFlagsBits.BanMembers); // }); } + + //Admin commands (Home Server only) + const guild = bot.guilds.cache.get(bot.home_server); + guild.commands.create({ + name: "admin", + description: "admin commands", + // type: Constants.ApplicationCommandOptionTypes.SUB_COMMAND_GROUP, + options: [ + { + name: "setpresence", + description: "Change the bot's presence", + type: Constants.ApplicationCommandOptionTypes.SUB_COMMAND, + options: [ + {name: "pres_text", description: "The new presence text", type: Constants.ApplicationCommandOptionTypes.STRING, required: true }, + {name: "type", description: "The new presence text", type: Constants.ApplicationCommandOptionTypes.STRING, required: true, choices: [ + {name: "LISTENING", value: "LISTENING"}, {name: "WATCHING", value: "WATCHING"}, {name: "COMPETING", value: "COMPETING"}, {name: "PLAYING", value: "PLAYING"}, { name: "STREAMING", value: "STREAMING"} + ]}, + {name: 'display_name', description: "What to display instead of the stream's title", type: Constants.ApplicationCommandOptionTypes.STRING, required: false} + ], + dm_permission: false + }, + { + name: "setactivity", + description: "Change the bot's activity", + type: Constants.ApplicationCommandOptionTypes.SUB_COMMAND, + options: [ + {name: "type", description: "The new presence text", type: Constants.ApplicationCommandOptionTypes.STRING, required: true, choices: [ + {name: "Do Not Disturb", value: "dnd"}, {name: "Idle", value: "idle"}, {name: "invisible", value: "invisible"}, {name: "online", value: "online"} + ]}, + ], + dm_permission: false + }, + ] + }); }