Skip to content

Commit

Permalink
Add blacklist slash command
Browse files Browse the repository at this point in the history
  • Loading branch information
alexemanuelol committed Aug 30, 2023
1 parent 53d4407 commit c6ef42a
Show file tree
Hide file tree
Showing 19 changed files with 487 additions and 94 deletions.
18 changes: 18 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Slash Command | Description
------------- | -----------
[**/alarm**](commands.md#alarm) | Operations on Smart Alarms.
[**/blacklist**](commands.md#blacklist) | Blacklist a user from using the bot.
[**/cctv**](commands.md#cctv) | Posts CCTV codes for a monument.
[**/credentials**](commands.md#credentials) | Set/Clear the FCM Credentials for the user account.
[**/help**](commands.md#help) | Display help message.
Expand Down Expand Up @@ -38,6 +39,23 @@ Subcommand | Options | Description | Required
![Discord Slash Command alarm Image](images/alarms_edit.png)


## **/blacklist**

> **Blacklist a user from using the bot.**
Subcommand | Options | Description | Required
---------- | ------- | ----------- | --------
`add` |   | Add user to the blacklist. |  
  | `discord_user` | The discord user. | `False`
  | `steamid` | The steamid of the user. | `False`
`remove` |   | Remove user from the blacklist. |  
  | `discord_user` | The discord user. | `False`
  | `steamid` | The steamid of the user. | `False`
`show` |   | Show blacklisted users. |  

![Discord Slash Command blacklist Image](images/blacklist.png)


## **/cctv**

> **Posts CCTV codes for a monument.**
Expand Down
3 changes: 2 additions & 1 deletion docs/full_list_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@
* Translate messages from different languages from teamchat.
* Send a Text-To-Speech message from in-game to Discord teamchat channel.
* Check the upkeep of all Tool Cupboard via the `upkeep` command.
* Get Facepunch news in Discord.
* Get Facepunch news in Discord.
* Blacklist users from using the bot.
Binary file added docs/images/blacklist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
252 changes: 252 additions & 0 deletions src/commands/blacklist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/*
Copyright (C) 2023 Alexander Emanuelsson (alexemanuelol)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
https://github.com/alexemanuelol/rustplusplus
*/

const Builder = require('@discordjs/builders');

const Constants = require('../util/constants.js');
const DiscordEmbeds = require('../discordTools/discordEmbeds.js');
const DiscordTools = require('../discordTools/discordTools.js');
const PermissionHandler = require('../handlers/permissionHandler.js');
const Scrape = require('../util/scrape.js');

module.exports = {
name: 'blacklist',

getData(client, guildId) {
return new Builder.SlashCommandBuilder()
.setName('blacklist')
.setDescription(client.intlGet(guildId, 'commandsBlacklistDesc'))
.addSubcommand(subcommand => subcommand
.setName('add')
.setDescription(client.intlGet(guildId, 'commandsBlacklistAddDesc'))
.addUserOption(option => option
.setName('discord_user')
.setDescription(client.intlGet(guildId, 'commandsBlacklistDiscordUserDesc'))
.setRequired(false))
.addStringOption(option => option
.setName('steamid')
.setDescription(client.intlGet(guildId, 'commandsBlacklistSteamidDesc'))
.setRequired(false)))
.addSubcommand(subcommand => subcommand
.setName('remove')
.setDescription(client.intlGet(guildId, 'commandsBlacklistRemoveDesc'))
.addUserOption(option => option
.setName('discord_user')
.setDescription(client.intlGet(guildId, 'commandsBlacklistDiscordUserDesc'))
.setRequired(false))
.addStringOption(option => option
.setName('steamid')
.setDescription(client.intlGet(guildId, 'commandsBlacklistSteamidDesc'))
.setRequired(false)))
.addSubcommand(subcommand => subcommand
.setName('show')
.setDescription(client.intlGet(guildId, 'commandsBlacklistShowDesc')));
},

async execute(client, interaction) {
const guildId = interaction.guildId;
const instance = client.getInstance(guildId);

if (!await client.validatePermissions(interaction)) return;

if (!client.isAdministrator(interaction)) {
const str = client.intlGet(guildId, 'missingPermission');
await client.interactionReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(null, 'warningCap'), str);
return;
}

await interaction.deferReply({ ephemeral: true });

const guild = DiscordTools.getGuild(guildId);

switch (interaction.options.getSubcommand()) {
case 'add': {
const discordUser = interaction.options.getUser('discord_user');
const steamid = interaction.options.getString('steamid');

if (discordUser === null && steamid === null) {
const str = client.intlGet(guildId, 'missingArguments');
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(null, 'warningCap'), str);
return;
}

let successful = 0;

let str = '';
if (discordUser !== null) {
if (instance.blacklist['discordIds'].includes(discordUser.id)) {
str += client.intlGet(guildId, 'userAlreadyInBlacklist', {
user: `${discordUser.username} (${discordUser.id})`
}) + ' ';
successful = 1;
}
else {
instance.blacklist['discordIds'].push(discordUser.id);
client.setInstance(guildId, instance);

await PermissionHandler.resetPermissionsAllChannels(client, guild);

str += client.intlGet(guildId, 'userAddedToBlacklist', {
user: `${discordUser.username} (${discordUser.id})`
}) + ' ';
}
}

if (steamid !== null) {
let name = '';
const steamName = await Scrape.scrapeSteamProfileName(client, steamid);
if (steamName) name += `${steamName} (${steamid})`;
else name += `${steamid}`;

if (instance.blacklist['steamIds'].includes(steamid)) {
str += client.intlGet(guildId, 'userAlreadyInBlacklist', {
user: name
}) + ' ';
successful = 1;
}
else {
instance.blacklist['steamIds'].push(steamid);
client.setInstance(guildId, instance);
str += client.intlGet(guildId, 'userAddedToBlacklist', {
user: name
}) + ' ';
}
}

await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(successful, str));
client.log(client.intlGet(null, 'infoCap'), str);
return;
} break;

case 'remove': {
const discordUser = interaction.options.getUser('discord_user');
const steamid = interaction.options.getString('steamid');

if (discordUser === null && steamid === null) {
const str = client.intlGet(guildId, 'missingArguments');
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(null, 'warningCap'), str);
return;
}

let successful = 0;

let str = '';
if (discordUser !== null) {
if (!instance.blacklist['discordIds'].includes(discordUser.id)) {
str += client.intlGet(guildId, 'userNotInBlacklist', {
user: `${discordUser.username} (${discordUser.id})`
}) + ' ';
successful = 1;
}
else {
instance.blacklist['discordIds'] =
instance.blacklist['discordIds'].filter(e => e !== discordUser.id)
client.setInstance(guildId, instance);

await PermissionHandler.resetPermissionsAllChannels(client, guild);

str += client.intlGet(guildId, 'userRemovedFromBlacklist', {
user: `${discordUser.username} (${discordUser.id})`
}) + ' ';
}
}

if (steamid !== null) {
let name = '';
const steamName = await Scrape.scrapeSteamProfileName(client, steamid);
if (steamName) name += `${steamName} (${steamid})`;
else name += `${steamid}`;

if (!instance.blacklist['steamIds'].includes(steamid)) {
str += client.intlGet(guildId, 'userNotInBlacklist', {
user: name
}) + ' ';
successful = 1;
}
else {
instance.blacklist['steamIds'] =
instance.blacklist['steamIds'].filter(e => e !== steamid)
client.setInstance(guildId, instance);
str += client.intlGet(guildId, 'userRemovedFromBlacklist', {
user: name
}) + ' ';
}
}


await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(successful, str));
client.log(client.intlGet(null, 'infoCap'), str);
return;
} break;

case 'show': {
let discordUsers = '';
let steamIds = '';

for (const discordId of instance.blacklist['discordIds']) {
const user = await DiscordTools.getUserById(guildId, discordId);
let name = '';
if (user) name = `${user.user.username} (${user.id})`;
else name = `${discordId}`;

discordUsers += `${name}\n`;
}

for (const steamId of instance.blacklist['steamIds']) {
let name = '';
const steamName = await Scrape.scrapeSteamProfileName(client, steamId);
if (steamName) name = `${steamName} (${steamId})`;
else name = `${steamId}`;

steamIds += `${name}\n`;
}

await client.interactionEditReply(interaction, {
embeds: [DiscordEmbeds.getEmbed({
color: Constants.COLOR_DEFAULT,
title: client.intlGet(interaction.guildId, 'blacklist'),
fields: [
{
name: client.intlGet(interaction.guildId, 'discordUsers'),
value: discordUsers === '' ? '\u200B' : discordUsers,
inline: true
},
{
name: 'SteamId',
value: steamIds === '' ? '\u200B' : steamIds,
inline: true
}]
})],
ephemeral: true
});

client.log(client.intlGet(guildId, 'infoCap'), client.intlGet(interaction.guildId, 'showingBlacklist'));
} break;

default: {
} break;
}

return;
},
};
2 changes: 2 additions & 0 deletions src/commands/cctv.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ module.exports = {
},

async execute(client, interaction) {
if (!await client.validatePermissions(interaction)) return;

const monument = interaction.options.getString('monument');
const cctvCodes = client.cctv.getCodes(monument);
const dynamic = client.cctv.isDynamic(monument);
Expand Down
2 changes: 2 additions & 0 deletions src/commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ module.exports = {
},

async execute(client, interaction) {
if (!await client.validatePermissions(interaction)) return;

await DiscordMessages.sendHelpMessage(interaction);
client.log(client.intlGet(null, 'infoCap'), client.intlGet(interaction.guildId, 'commandsHelpDesc'));
},
Expand Down
Loading

0 comments on commit c6ef42a

Please sign in to comment.