4 Commits

Author SHA1 Message Date
Logan Cusano
d8a697e583 #13
- Added command to allow users to give other users their group
- Shows all groups as options but will not let user add another user if the caller doesn't have the role
2023-06-17 21:49:46 -04:00
Logan Cusano
44caa11f7c Fixed typo in remove description 2023-06-17 21:28:33 -04:00
Logan Cusano
dc92b07426 Implemented function to remove commands from a given bot and guild 2023-06-17 19:37:13 -04:00
Logan Cusano
92f4caad0c Improve the join node verification system 2023-06-17 19:25:47 -04:00
4 changed files with 99 additions and 15 deletions

View File

@@ -0,0 +1,52 @@
const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "give-role");
module.exports = {
data: new SlashCommandBuilder()
.setName('give-role')
.setDescription('Use this command to give a role you have to another member.')
.addUserOption(option =>
option.setName('user')
.setDescription('The user you wish to give the role to ')
.setRequired(true))
.addRoleOption(option =>
option.setName('role')
.setDescription('The role you wish to give the selected user')
.setRequired(true)),
example: "give-role",
isPrivileged: false,
requiresTokens: false,
defaultTokenUsage: 0,
deferInitialReply: true,
/*async autocomplete(interaction) {
const focusedValue = interaction.options.getFocused();
},*/
async execute(interaction) {
try{
// The role to give to the user
const selectedRole = interaction.options.getRole('role');
// The user who should be given the role
var selectedUser = interaction.options.getUser("user");
selectedUser = interaction.guild.members.cache.get(selectedUser.id);
// The user who initiated the command
const initUser = interaction.member;
log.DEBUG("Give Role DEBUG: ", initUser, selectedRole, selectedUser);
// Check if the user has the role selected
if (!initUser.roles.cache.find(role => role.name === selectedRole.name)) return await interaction.editReply(`Sorry ${initUser}, you don't have the group ${selectedRole} and thus you cannot give it to ${selectedUser}`);
// Give the selected user the role and let both the user and the initiator know
await selectedUser.roles.add(selectedRole);
return await interaction.editReply(`Ok ${initUser}, ${selectedUser} has been given the ${selectedRole} role!`)
}catch(err){
log.ERROR(err)
//await interaction.reply(err.toString());
}
}
};

View File

@@ -3,7 +3,7 @@ const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder"); const { DebugBuilder } = require("../utilities/debugBuilder");
const { getMembersInRole, getAllClientIds, filterAutocompleteValues } = require("../utilities/utils"); const { getMembersInRole, getAllClientIds, filterAutocompleteValues } = require("../utilities/utils");
const { requestOptions, sendHttpRequest } = require("../utilities/httpRequests"); const { requestOptions, sendHttpRequest } = require("../utilities/httpRequests");
const { getOnlineNodes, updateNodeInfo, addNodeConnection, getConnectionByNodeId } = require("../utilities/mysqlHandler"); const { getOnlineNodes, updateNodeInfo, addNodeConnection, getConnectionByNodeId, getAllConnections } = require("../utilities/mysqlHandler");
// Global Vars // Global Vars
const log = new DebugBuilder("server", "join"); const log = new DebugBuilder("server", "join");
@@ -13,10 +13,10 @@ const log = new DebugBuilder("server", "join");
* *
* @param {*} presetName The preset name to listen to on the client * @param {*} presetName The preset name to listen to on the client
* @param {*} channelId The channel ID to join the bot to * @param {*} channelId The channel ID to join the bot to
* @param {*} clientIdsUsed EITHER A collection of clients that are currently connected OR a single discord client ID (NOT dev portal ID) that should be used to join the server with * @param {*} connections EITHER A collection of clients that are currently connected OR a single discord client ID (NOT dev portal ID) that should be used to join the server with
* @returns * @returns
*/ */
async function joinServerWrapper(presetName, channelId, clientIdsUsed) { async function joinServerWrapper(presetName, channelId, connections) {
// Get nodes online // Get nodes online
var onlineNodes = await new Promise((recordResolve, recordReject) => { var onlineNodes = await new Promise((recordResolve, recordReject) => {
getOnlineNodes((nodeRows) => { getOnlineNodes((nodeRows) => {
@@ -45,16 +45,16 @@ async function joinServerWrapper(presetName, channelId, clientIdsUsed) {
log.DEBUG("All clients: ", Object.keys(availableClientIds)); log.DEBUG("All clients: ", Object.keys(availableClientIds));
var selectedClientId; var selectedClientId;
if (typeof clientIdsUsed === 'string') { if (typeof connections === 'string') {
for (const availableClientId of availableClientIds) { for (const availableClientId of availableClientIds) {
if (availableClientId.discordId != clientIdsUsed ) selectedClientId = availableClientId; if (availableClientId.discordId != connections ) selectedClientId = availableClientId;
} }
} }
else { else {
log.DEBUG("Client IDs Used: ", clientIdsUsed.keys()); log.DEBUG("Open connections: ", connections);
for (const usedClientId of clientIdsUsed.keys()) { for (const connection of connections) {
log.DEBUG("Used Client ID: ", usedClientId); log.DEBUG("Used Client ID: ", connection);
availableClientIds = availableClientIds.filter(cid => cid.discordId != usedClientId); availableClientIds = availableClientIds.filter(cid => cid.discordId != connection.clientObject.discordId);
} }
log.DEBUG("Available Client IDs: ", availableClientIds); log.DEBUG("Available Client IDs: ", availableClientIds);
@@ -134,11 +134,11 @@ module.exports = {
const channelId = interaction.member.voice.channel.id; const channelId = interaction.member.voice.channel.id;
log.DEBUG(`Join requested by: ${interaction.user.username}, to: '${presetName}', in channel: ${channelId} / ${guildId}`); log.DEBUG(`Join requested by: ${interaction.user.username}, to: '${presetName}', in channel: ${channelId} / ${guildId}`);
const onlineBots = await getMembersInRole(interaction); const connections = await getAllConnections();
log.DEBUG("Online Bots: ", onlineBots); log.DEBUG("Current Connections: ", connections);
const selectedClientId = await joinServerWrapper(presetName, channelId, onlineBots.online); const selectedClientId = await joinServerWrapper(presetName, channelId, connections);
await interaction.editReply(`Ok, ${interaction.member}. **${selectedClientId.name}** is joining your channel.`); await interaction.editReply(`Ok, ${interaction.member}. **${selectedClientId.name}** is joining your channel.`);
//await interaction.channel.send('**Pong.**'); // This will send a message to the channel of the interaction outside of the initial reply //await interaction.channel.send('**Pong.**'); // This will send a message to the channel of the interaction outside of the initial reply

View File

@@ -7,7 +7,7 @@ const log = new DebugBuilder("server", "remove");
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName('remove') .setName('remove')
.setDescription('Remove an RSS source by it\' title') .setDescription('Remove an RSS source by it\'s title')
.addStringOption(option => .addStringOption(option =>
option.setName('title') option.setName('title')
.setDescription('The title of the source to remove') .setDescription('The title of the source to remove')

View File

@@ -11,14 +11,14 @@ const path = require('node:path');
const { DebugBuilder } = require("./debugBuilder"); const { DebugBuilder } = require("./debugBuilder");
const log = new DebugBuilder("server", "deployCommands"); const log = new DebugBuilder("server", "deployCommands");
const commands = []; var commands = [];
// Grab all the command files from the commands directory you created earlier // Grab all the command files from the commands directory you created earlier
const commandsPath = path.resolve(__dirname, '../commands'); const commandsPath = path.resolve(__dirname, '../commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
exports.deploy = (clientId, guildIDs) => { exports.deploy = (clientId, guildIDs) => {
log.DEBUG("Deploying commands for: ", guildIDs); log.DEBUG("Deploying commands for: ", guildIDs);
if (Array.isArray(guildIDs)) guildIDs = [guildIDs]; if (!Array.isArray(guildIDs)) guildIDs = [guildIDs];
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) { for (const file of commandFiles) {
const command = require(`${path.resolve(commandsPath, file)}`); const command = require(`${path.resolve(commandsPath, file)}`);
@@ -48,3 +48,35 @@ exports.deploy = (clientId, guildIDs) => {
})() })()
} }
}; };
/**
* Remove all commands for a given bot in a given guild
*
* @param {*} clientId The client ID of the bot to remove commands from
* @param {*} guildId The ID of the guild to remove the bot commands from
*/
exports.removeAll = (clientId, guildId) => {
if (!Array.isArray(guildId)) guildIDs = [guildId];
log.DEBUG("Removing commands for: ", clientId, guildIDs);
commands = [];
const rest = new REST({ version: '10' }).setToken(token);
for (const guildId of guildIDs){
(async () => {
try {
log.DEBUG(`Started refreshing ${commands.length} application (/) commands for guild ID: ${guildId}.`);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);
log.DEBUG(`Successfully reloaded ${data.length} application (/) commands for guild ID: ${guildId}.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
log.ERROR("ERROR Deploying commands: ", error, "Body from error: ", commands);
}
})()
}
}