diff --git a/commands/debug.js b/commands/debug.js new file mode 100644 index 0000000..52c81da --- /dev/null +++ b/commands/debug.js @@ -0,0 +1,133 @@ +const { MessageEmbed } = require('discord.js'); +const io = require('socket.io-client'); + +module.exports = { + name: "debug", + description: "Debug for bot admin", + usage: "command", + permissions: { + channel: ["VIEW_CHANNEL", "SEND_MESSAGES", "EMBED_LINKS"], + member: [], + }, + aliases: [], + /** + * + * @param {require("../structures/LinesPoliceCadBot")} client + * @param {import("discord.js").Message} message + * @param {string[]} args + * @param {*} param3 + */ + run: async (client, message, args, { GuildDB }) => { + if (!client.config.Admins.includes(message.author.id)) return; + + if (args.length==0) return message.channel.send('Debug Error: Provide a debug command'); + if (!client.exists(args[0])) return message.channel.send('Debug Error: Provide a valid debug command'); + + let debugConsole = false + if (client.exists(args[1])&&args[1]=='true') debugConsole = true + + let command = args[0].toLowerCase(); + + if (command == 'server' || command == 'guild') { + if (debugConsole) { + client.log("Debug: Guild >> "); + console.debug(GuildDB); + } + return message.channel.send("Debug: guild >> read log output") + + } else if (command == "interaction") { + return message.channel.send(`Use slash command to debug \`interaction\``); + + } else if (command == "message") { + if (debugConsole) { + client.log("Debug: message >> "); + console.debug(message); + } + return message.channel.send("Debug: message >> read log output"); + + } else if (command == "pingserver") { + const socket = io.connect(client.config.socket); + socket.emit('botping', {message:'hello there'}); + socket.on('botpong', (data) => { + if (debugConsole) client.log('Debug: Socket responded') + return message.channel.send(`Debug: Socket responded`) + socket.disconnect(); + }); + + } else if (command == "apicheck") { + // TODO: Make axios call to api and get api health + return message.channel.send('Debug Notice: \`apicheck\` is currently unavailable'); + + } else { + return message.channel.send('Debug Error: Provide a valid debug command'); + + } + }, + SlashCommand: { + options: [ + { + name: "command", + description: "The debug command to execute", + value: "command", + type: 3, + required: true, + }, + { + name: "consoleoutput", + description: "Output debug messages to console (deault=false)", + value: "consoleoutput", + type: 5, + required: true, + }, + ], + /** + * + * @param {require("../structures/LinesPoliceCadBot")} client + * @param {import("discord.js").Message} message + * @param {string[]} args + * @param {*} param3 + */ + run: async (client, interaction, args, { GuildDB }) => { + if (!client.config.Admins.includes(interaction.member.user.id)) return; + + if (args.length==0) return message.channel.send('Debug Error: Provide a debug command'); + if (!client.exists(args[0].value)) return interaction.send('Debug Error: Provide a valid debug command'); + + let debugConsole = args[1].value; + + let command = args[0].value.toLowerCase(); + + if (command == 'server' || args[0] == 'guild') { + if (debugConsole) client.log("Debug: Guild >> ");console.debug(GuildDB); + return interaction.send("Debug: guild >> read log output") + + } else if (command == "interaction") { + if (debugConsole) { + client.log("Debug: interaction >> "); + console.debug(interaction); + } + return interaction.send("Debug: interaction >> read log output"); + + } else if (command == "message") { + return interaction.send(`Use command with prefix to debug \`message\``); + + } else if (command == "pingserver") { + const socket = io.connect(client.config.socket); + socket.emit('botping', {message:'hello there'}); + socket.on('botpong', (data) => { + if (debugConsole) client.log('Debug: Socket responded') + return interaction.send(`Debug: Socket responded`) + socket.disconnect(); + }); + + } else if (command == "apicheck") { + // TODO: Make axios call to api and get api health + return interaction.send('Debug Notice: \`apicheck\` is currently unavailable'); + + } else { + return interaction.send('Debug Error: Provide a valid debug command'); + + } + }, + }, +} diff --git a/commands/help.js b/commands/help.js index c469a02..3c057f5 100644 --- a/commands/help.js +++ b/commands/help.js @@ -17,11 +17,10 @@ module.exports = { * @param {*} param3 */ run: async (client, message, args, { GuildDB }) => { - let Commands = client.commands.map( - (cmd) => - `\`${GuildDB ? GuildDB.prefix : client.config.DefaultPrefix}${ + let Commands = client.commands.map((cmd) => + cmd.name != 'debug' ? `\`${GuildDB ? GuildDB.prefix : client.config.DefaultPrefix}${ cmd.name - }${cmd.usage ? " " + cmd.usage : ""}\` - ${cmd.description}` + }${cmd.usage ? " " + cmd.usage : ""}\` - ${cmd.description}` : null ); let Embed = new MessageEmbed() @@ -101,11 +100,10 @@ module.exports = { if (GuildDB.customChannelStatus==true&&!GuildDB.allowedChannels.includes(interaction.channel_id)) { return interaction.send(`You are not allowed to use the bot in this channel.`); } - let Commands = client.commands.map( - (cmd) => - `\`${GuildDB ? GuildDB.prefix : client.config.DefaultPrefix}${ + let Commands = client.commands.map((cmd) => + cmd.name != 'debug' ? `\`${GuildDB ? GuildDB.prefix : client.config.DefaultPrefix}${ cmd.name - }${cmd.usage ? " " + cmd.usage : ""}\` - ${cmd.description}` + }${cmd.usage ? " " + cmd.usage : ""}\` - ${cmd.description}` : null ); let Embed = new MessageEmbed() diff --git a/commands/nameSearch.js b/commands/nameSearch.js index 0b3227a..81514e8 100644 --- a/commands/nameSearch.js +++ b/commands/nameSearch.js @@ -1,7 +1,6 @@ const { MessageEmbed } = require('discord.js'); const io = require('socket.io-client'); - module.exports = { name: "namesearch", description: "Search a Civilian in your community", @@ -18,7 +17,13 @@ module.exports = { * @param {string[]} args * @param {*} param3 */ - run: async (client, message, args) => { + run: async (client, message, args, { GuildDB }) => { + if (GuildDB.customRoleStatus) { + let userRoles = []; // TODO: get user roles + let hasRole = await client.hasRole(userRoles, GuildDB.serverID); + if (!hasRole) return message.channel.send(`You don't have permission to use this command.`); + } + let user = await client.dbo.collection("users").findOne({"user.discord.id":message.author.id}).then(user => user); if (!user) return message.channel.send(`You are not logged in.`); let data; diff --git a/config/beta-config.js b/config/beta-config.js index e11b280..2761ab8 100644 --- a/config/beta-config.js +++ b/config/beta-config.js @@ -1,7 +1,7 @@ module.exports = { Dev: "DEVELOPMENT", Version: "2.0.0 Beta", - Admins: ["userID", "userID"], // Admins of the bot + Admins: ["362791661274660874"], // Admins of the bot defaultPrefix: "?", socket: "https://police-cad-dev.herokuapp.com/", SupportServer: "https://discord.gg/w2g2FFmHbF", //Support Server Link diff --git a/config/config.js b/config/config.js index f3a86e4..4f2389e 100644 --- a/config/config.js +++ b/config/config.js @@ -1,7 +1,7 @@ module.exports = { Dev: "PRODUCTION", Version: "2.0.0", - Admins: ["userID", "userID"], // Admins of the bot + Admins: ["362791661274660874"], // Admins of the bot defaultPrefix: "?", socket: "https://www.linespolice-cad.com/", SupportServer: "https://discord.gg/w2g2FFmHbF", //Support Server Link diff --git a/structures/LinesPoliceCadBot.js b/structures/LinesPoliceCadBot.js index b80ba29..a8fe82d 100644 --- a/structures/LinesPoliceCadBot.js +++ b/structures/LinesPoliceCadBot.js @@ -75,6 +75,8 @@ class LinesPoliceCadBot extends Client { this.log('Successfully connected to mongoDB'); } + exists(n){return null!=n&&null!=n&&""!=n} + LoadCommands() { let CommandsDir = path.join(__dirname, '..', 'commands'); fs.readdir(CommandsDir, (err, files) => { @@ -121,12 +123,15 @@ class LinesPoliceCadBot extends Client { }); } - async checkRoleStatus(message) { + async checkRoleStatus(roles, serverID) { let hasRole = false; - let guild = await this.dbo.collection("prefixes").findOne({"server.serverID":message.guild.id}).then(guild => guild); + let guild = await this.dbo.collection("prefixes").findOne({"server.serverID":serverID}).then(guild => guild); // If user has one of any in the list of allowed roles, hasRole is true for (let i = 0; i < guild.server.allowedRoles.length; i++) { - if (message.member.roles.cache.some(role => role.id == guild.server.allowedRoles[i])) hasRole = true; + if (roles.includes(guild.server.allowedRoles[i])) { + hasRole = true + break + } } return hasRole; }