refactor(arabot): remove unnecessary checks for finding users

This commit is contained in:
smyalygames 2023-03-15 00:48:27 +00:00
parent c6628140ac
commit 0e0b8b2d02
13 changed files with 77 additions and 130 deletions

View File

@ -62,16 +62,14 @@ export class AccessCommand extends Command {
// Command run
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
// Get the arguments
const permission = interaction.options.getString('permission');
const parsedChannel = interaction.options.getChannel('channel');
const permission = interaction.options.getString('permission', true);
const parsedChannel = interaction.options.getChannel('channel', true);
const user = interaction.options.getUser('user');
const role = interaction.options.getRole('role');
const { guild } = interaction;
// Checks if all the variables are of the right type
if (permission === null
|| parsedChannel === null
|| guild === null
if (guild === null
|| (user === null && role === null)) {
await interaction.reply({
content: 'Error fetching slash command data!',

View File

@ -73,34 +73,34 @@ export class PrivateCommand extends Subcommand {
public async create(interaction: Subcommand.ChatInputCommandInteraction) {
// Get the arguments
const user = interaction.options.getUser('user');
const mod = interaction.member;
const user = interaction.options.getUser('user', true);
const modUser = interaction.user;
const { guild } = interaction;
await interaction.deferReply({ ephemeral: true });
// Checks if all the variables are of the right type
if (user === null || guild === null || mod === null) {
if (guild === null) {
await interaction.editReply({
content: 'Error fetching user!',
content: 'Error fetching mod!',
});
return;
}
const guildMember = guild.members.cache.get(user.id);
const modGuildMember = guild.members.cache.get(mod.user.id);
const member = guild.members.cache.get(user.id);
const mod = guild.members.cache.get(modUser.id);
// Checks if guildMember is null
if (guildMember === undefined || modGuildMember === undefined) {
if (member === undefined || mod === undefined) {
await interaction.editReply({
content: 'Error fetching users!',
});
return;
}
const [name, coordinator] = this.getCoordinator(modGuildMember);
const [name, coordinator] = this.getCoordinator(mod);
if (this.checkPrivate(guildMember.id, coordinator, guild)) {
if (this.checkPrivate(member.id, coordinator, guild)) {
await interaction.editReply({
content: 'A private channel already exists!',
});
@ -134,7 +134,7 @@ export class PrivateCommand extends Subcommand {
let bannedName = false;
try {
privateChannel = await guild.channels.create({
name: `🍂┃${guildMember.user.username}-private-${name}`,
name: `🍂┃${member.user.username}-private-${name}`,
type: ChannelType.GuildText,
topic: `Private channel. ${user.id} ${coordinator} ${voiceChannel.id} (Please do not change this)`,
parent: IDs.categories.private,
@ -157,7 +157,7 @@ export class PrivateCommand extends Subcommand {
});
} catch {
privateChannel = await guild.channels.create({
name: `🍂┃${guildMember.user.id}-private-${name}`,
name: `🍂┃${member.user.id}-private-${name}`,
type: ChannelType.GuildText,
topic: `Private channel. ${user.id} ${coordinator} ${voiceChannel.id} (Please do not change this)`,
parent: IDs.categories.private,
@ -182,19 +182,19 @@ export class PrivateCommand extends Subcommand {
}
if (!bannedName) {
await voiceChannel.setName(`${guildMember.user.username}-private-${name}`);
await voiceChannel.setName(`${member.user.username}-private-${name}`);
} else {
await voiceChannel.setName(`${guildMember.user.id}-private-${name}`);
await voiceChannel.setName(`${member.user.id}-private-${name}`);
}
const joinTime = time(guildMember.joinedAt!);
const registerTime = time(guildMember.user.createdAt);
const joinTime = time(member.joinedAt!);
const registerTime = time(member.user.createdAt);
const embed = new EmbedBuilder()
.setColor(guildMember.displayHexColor)
.setTitle(`Private channel for ${guildMember.user.username}`)
.setDescription(`${guildMember}`)
.setThumbnail(guildMember.user.displayAvatarURL())
.setColor(member.displayHexColor)
.setTitle(`Private channel for ${member.user.username}`)
.setDescription(`${member}`)
.setThumbnail(member.user.displayAvatarURL())
.addFields(
{ name: 'Joined:', value: `${joinTime}`, inline: true },
{ name: 'Created:', value: `${registerTime}`, inline: true },
@ -210,30 +210,30 @@ export class PrivateCommand extends Subcommand {
public async delete(interaction: Subcommand.ChatInputCommandInteraction) {
// Get the arguments
const user = interaction.options.getUser('user');
const mod = interaction.member;
const modUser = interaction.user;
const { guild, channel } = interaction;
await interaction.deferReply({ ephemeral: true });
// Checks if all the variables are of the right type
if (mod === null || guild === null || channel === null) {
if (guild === null || channel === null) {
await interaction.editReply({
content: 'Error fetching user!',
});
return;
}
const modGuildMember = guild.members.cache.get(mod.user.id);
const mod = guild.members.cache.get(modUser.id);
// Checks if guildMember is null
if (modGuildMember === undefined) {
if (mod === undefined) {
await interaction.editReply({
content: 'Error fetching users!',
});
return;
}
const coordinatorInfo = this.getCoordinator(modGuildMember);
const coordinatorInfo = this.getCoordinator(mod);
const coordinator = coordinatorInfo[1];
let topic: string[];

View File

@ -64,13 +64,13 @@ export class BanCommand extends Command {
// Get the arguments
const user = interaction.options.getUser('user', true);
const reason = interaction.options.getString('reason', true);
const mod = interaction.member;
const mod = interaction.user;
const { guild } = interaction;
// Checks if all the variables are of the right type
if (guild === null || mod === null) {
if (guild === null) {
await interaction.reply({
content: 'Error fetching user!',
content: 'Error fetching guild!',
ephemeral: true,
fetchReply: true,
});
@ -79,7 +79,7 @@ export class BanCommand extends Command {
await interaction.deferReply();
const ban = await this.ban(user.id, mod.user.id, reason, guild);
const ban = await this.ban(user.id, mod.id, reason, guild);
await interaction.editReply({ content: ban.message });
}
@ -96,7 +96,7 @@ export class BanCommand extends Command {
return;
}
const reason = args.finished ? null : await args.rest('string');
const mod = message.member;
const mod = message.author;
if (reason === null) {
await message.react('❌');
@ -104,12 +104,6 @@ export class BanCommand extends Command {
return;
}
if (mod === null) {
await message.react('❌');
await message.reply('Moderator not found! Try again or contact a developer!');
return;
}
const { guild } = message;
if (guild === null) {
@ -125,7 +119,7 @@ export class BanCommand extends Command {
return;
}
const ban = await this.ban(user.id, mod.user.id, reason, guild);
const ban = await this.ban(user.id, mod.id, reason, guild);
await message.reply(ban.message);
await message.react(ban.success ? '✅' : '❌');

View File

@ -67,13 +67,13 @@ export class TempBanCommand extends Command {
const user = interaction.options.getUser('user', true);
const duration = interaction.options.getString('duration', true);
const reason = interaction.options.getString('reason', true);
const mod = interaction.member;
const mod = interaction.user;
const { guild } = interaction;
// Checks if all the variables are of the right type
if (guild === null || mod === null) {
if (guild === null) {
await interaction.reply({
content: 'Error fetching user!',
content: 'Error fetching guild!',
ephemeral: true,
fetchReply: true,
});
@ -91,7 +91,7 @@ export class TempBanCommand extends Command {
await interaction.deferReply();
const ban = await this.ban(user.id, mod.user.id, time, reason, guild);
const ban = await this.ban(user.id, mod.id, time, reason, guild);
await interaction.editReply({ content: ban.message });
}
@ -108,7 +108,7 @@ export class TempBanCommand extends Command {
return;
}
const arg = args.finished ? null : await args.rest('string');
const mod = message.member;
const mod = message.author;
if (arg === null) {
await message.react('❌');
@ -116,12 +116,6 @@ export class TempBanCommand extends Command {
return;
}
if (mod === null) {
await message.react('❌');
await message.reply('Moderator not found! Try again or contact a developer!');
return;
}
const { duration, reason } = this.findTimeAndReason(arg);
if (Number.isNaN(duration.offset)) {
@ -151,7 +145,7 @@ export class TempBanCommand extends Command {
return;
}
const ban = await this.ban(user.id, mod.user.id, duration, reason, guild);
const ban = await this.ban(user.id, mod.id, duration, reason, guild);
await message.reply(ban.message);
await message.react(ban.success ? '✅' : '❌');

View File

@ -61,13 +61,13 @@ export class UnbanCommand extends Command {
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
// Get the arguments
const user = interaction.options.getUser('user', true);
const mod = interaction.member;
const mod = interaction.user;
const { guild } = interaction;
// Checks if all the variables are of the right type
if (guild === null || mod === null) {
if (guild === null) {
await interaction.reply({
content: 'Error fetching user!',
content: 'Error fetching guild!',
ephemeral: true,
fetchReply: true,
});
@ -76,7 +76,7 @@ export class UnbanCommand extends Command {
await interaction.deferReply();
const unban = await this.unban(user.id, mod.user.id, guild);
const unban = await this.unban(user.id, mod.id, guild);
await interaction.editReply({ content: unban.message });
}
@ -93,13 +93,7 @@ export class UnbanCommand extends Command {
return;
}
const mod = message.member;
if (mod === null) {
await message.react('❌');
await message.reply('Moderator not found! Try again or contact a developer!');
return;
}
const mod = message.author;
const { guild } = message;
@ -109,7 +103,7 @@ export class UnbanCommand extends Command {
return;
}
const unban = await this.unban(user.id, mod.user.id, guild);
const unban = await this.unban(user.id, mod.id, guild);
await message.reply(unban.message);
await message.react(unban.success ? '✅' : '❌');

View File

@ -17,6 +17,8 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// TODO This file needs a MASSIVE refactor
import { Args, container, RegisterBehavior } from '@sapphire/framework';
import { Subcommand } from '@sapphire/plugin-subcommands';
import {

View File

@ -68,7 +68,7 @@ export class RenameUserCommand extends Command {
}
// Gets guildMember whilst removing the ability of each other variables being null
const member = guild.members.cache.get(user?.id);
const member = guild.members.cache.get(user.id);
// Checks if guildMember is null
if (member === undefined) {

View File

@ -303,13 +303,13 @@ export class RestrictCommand extends Command {
// Get the arguments
const user = interaction.options.getUser('user', true);
const reason = interaction.options.getString('reason', true);
const mod = interaction.member;
const mod = interaction.user;
const { guild } = interaction;
// Checks if all the variables are of the right type
if (guild === null || mod === null) {
if (guild === null) {
await interaction.reply({
content: 'Error fetching user!',
content: 'Error fetching guild!',
ephemeral: true,
fetchReply: true,
});
@ -318,7 +318,7 @@ export class RestrictCommand extends Command {
await interaction.deferReply();
const info = await restrictRun(user?.id, mod.user.id, reason, guild);
const info = await restrictRun(user?.id, mod.id, reason, guild);
await interaction.editReply({
content: info.message,
@ -337,7 +337,7 @@ export class RestrictCommand extends Command {
return;
}
const reason = args.finished ? null : await args.rest('string');
const mod = message.member;
const mod = message.author;
if (reason === null) {
await message.react('❌');
@ -345,12 +345,6 @@ export class RestrictCommand extends Command {
return;
}
if (mod === null) {
await message.react('❌');
await message.reply('Moderator not found! Try again or contact a developer!');
return;
}
const { guild } = message;
if (guild === null) {
@ -359,7 +353,7 @@ export class RestrictCommand extends Command {
return;
}
const info = await restrictRun(user?.id, mod.user.id, reason, guild);
const info = await restrictRun(user?.id, mod.id, reason, guild);
await message.reply(info.message);
await message.react(info.success ? '✅' : '❌');

View File

@ -55,13 +55,13 @@ export class RestrictToleranceCommand extends Command {
// Get the arguments
const user = interaction.options.getUser('user', true);
const reason = interaction.options.getString('reason', true);
const mod = interaction.member;
const mod = interaction.user;
const { guild } = interaction;
// Checks if all the variables are of the right type
if (guild === null || mod === null) {
if (guild === null) {
await interaction.reply({
content: 'Error fetching user!',
content: 'Error fetching guild!',
ephemeral: true,
fetchReply: true,
});
@ -70,7 +70,7 @@ export class RestrictToleranceCommand extends Command {
await interaction.deferReply();
const info = await restrictRun(user?.id, mod.user.id, reason, guild, true);
const info = await restrictRun(user?.id, mod.id, reason, guild, true);
await interaction.editReply({
content: info.message,
@ -89,7 +89,7 @@ export class RestrictToleranceCommand extends Command {
return;
}
const reason = args.finished ? null : await args.rest('string');
const mod = message.member;
const mod = message.author;
if (reason === null) {
await message.react('❌');
@ -97,12 +97,6 @@ export class RestrictToleranceCommand extends Command {
return;
}
if (mod === null) {
await message.react('❌');
await message.reply('Moderator not found! Try again or contact a developer!');
return;
}
const { guild } = message;
if (guild === null) {
@ -111,7 +105,7 @@ export class RestrictToleranceCommand extends Command {
return;
}
const info = await restrictRun(user?.id, mod.user.id, reason, guild, true);
const info = await restrictRun(user?.id, mod.id, reason, guild, true);
await message.reply(info.message);
await message.react(info.success ? '✅' : '❌');

View File

@ -66,29 +66,18 @@ export class RestrictToolsCommand extends Subcommand {
public async deleteChannel(interaction: Subcommand.ChatInputCommandInteraction) {
// Get the arguments
const user = interaction.options.getUser('user');
const modInteraction = interaction.member;
const { guild, channel } = interaction;
await interaction.deferReply({ ephemeral: true });
// Checks if all the variables are of the right type
if (modInteraction === null || guild === null || channel === null) {
if (guild === null || channel === null) {
await interaction.editReply({
content: 'Error fetching user!',
});
return;
}
const mod = guild.members.cache.get(modInteraction.user.id);
// Checks if guildMember is null
if (mod === undefined) {
await interaction.editReply({
content: 'Error fetching moderator!',
});
return;
}
let topic: string[];
if (user === null) {

View File

@ -60,13 +60,13 @@ export class UnRestrictCommand extends Command {
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
// Get the arguments
const user = interaction.options.getUser('user', true);
const mod = interaction.member;
const mod = interaction.user;
const { guild } = interaction;
// Checks if all the variables are of the right type
if (guild === null || mod === null) {
if (guild === null) {
await interaction.reply({
content: 'Error fetching user!',
content: 'Error fetching guild!',
ephemeral: true,
});
return;
@ -74,7 +74,7 @@ export class UnRestrictCommand extends Command {
await interaction.deferReply();
const info = await this.unRestrictRun(user?.id, mod.user.id, guild);
const info = await this.unRestrictRun(user?.id, mod.id, guild);
await interaction.editReply({
content: info.message,
@ -93,13 +93,7 @@ export class UnRestrictCommand extends Command {
return;
}
const mod = message.member;
if (mod === null) {
await message.react('❌');
await message.reply('Moderator not found! Try again or contact a developer!');
return;
}
const mod = message.author;
const { guild } = message;
@ -111,7 +105,7 @@ export class UnRestrictCommand extends Command {
const channelRun = message.channel;
const info = await this.unRestrictRun(user?.id, mod.user.id, guild, channelRun.id);
const info = await this.unRestrictRun(user?.id, mod.id, guild, channelRun.id);
if (!info.runInVeganRestrict) {
await message.reply(info.message);

View File

@ -55,13 +55,13 @@ export class VCMuteCommand extends Command {
// Get the arguments
const user = interaction.options.getUser('user', true);
const reason = interaction.options.getString('reason');
const modUser = interaction.member;
const modUser = interaction.user;
const { guild } = interaction;
// Checks if all the variables are of the right type
if (modUser === null || guild === null) {
if (guild === null) {
await interaction.reply({
content: 'Error fetching user!',
content: 'Error fetching guild!',
ephemeral: true,
fetchReply: true,
});
@ -70,7 +70,7 @@ export class VCMuteCommand extends Command {
// Gets guildMember whilst removing the ability of each other variables being null
const member = guild.members.cache.get(user.id);
const mod = guild.members.cache.get(modUser.user.id);
const mod = guild.members.cache.get(modUser.id);
// Checks if guildMember is null
if (member === undefined || mod === undefined) {

View File

@ -62,21 +62,21 @@ export class VerifyCommand extends Command {
// Get the arguments
const user = interaction.options.getUser('user', true);
const roles = interaction.options.getString('roles', true);
const verifier = interaction.member;
const verifier = interaction.user;
const { guild } = interaction;
const messageId = interaction.id;
// Checks if all the variables are of the right type
if (verifier === null || guild === null) {
if (guild === null) {
await interaction.reply({
content: 'Error fetching moderator or guild!',
content: 'Error fetching guild!',
ephemeral: true,
fetchReply: true,
});
return;
}
const verify = await this.verify(user, verifier.user.id, roles, messageId, guild);
const verify = await this.verify(user, verifier.id, roles, messageId, guild);
await interaction.reply({
content: verify.message,
@ -103,13 +103,7 @@ export class VerifyCommand extends Command {
return;
}
const verifier = message.member;
if (verifier === null) {
await message.react('❌');
await message.reply('Verifier not found! Try again or contact a developer!');
return;
}
const verifier = message.author;
const { guild } = message;
@ -119,7 +113,7 @@ export class VerifyCommand extends Command {
return;
}
const verify = await this.verify(user, verifier.user.id, roles, message.id, guild);
const verify = await this.verify(user, verifier.id, roles, message.id, guild);
await message.reply(verify.message);
await message.react(verify.success ? '✅' : '❌');