Skip to content

Commit

Permalink
multitude of fixes and removal of duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsundso committed Oct 30, 2023
1 parent 3218ef8 commit 24814e0
Show file tree
Hide file tree
Showing 43 changed files with 372 additions and 380 deletions.
2 changes: 0 additions & 2 deletions src/BaseClient/ClientHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ import {
token as getBotIdFromToken,
guild as getBotIdFromGuild,
} from './ClientHelperModules/getBotIdFrom.js';
import getCustomBot from './ClientHelperModules/getCustomBot.js';
import isModeratable from './ClientHelperModules/isModeratable.js';
import fetchAllEventSubscribers from './ClientHelperModules/fetchAllEventSubscribers.js';
import fetchAllGuildMembers from './ClientHelperModules/fetchAllGuildMembers.js';
Expand Down Expand Up @@ -107,7 +106,6 @@ export {
fetchAllEventSubscribers,
request,
isModeratable,
getCustomBot,
getBotIdFromToken,
getBotIdFromGuild,
requestHandler,
Expand Down
1 change: 1 addition & 0 deletions src/BaseClient/ClientHelperModules/cache/discord/pins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Discord from 'discord.js';
// eslint-disable-next-line import/no-cycle
import error from '../../error.js';
// eslint-disable-next-line import/no-cycle
import getBotMemberFromGuild from '../../getBotMemberFromGuild.js';
Expand Down
27 changes: 17 additions & 10 deletions src/BaseClient/ClientHelperModules/getBotMemberFromGuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ import * as Discord from 'discord.js';
import { guild as getBotIdFromGuild } from './getBotIdFrom.js';
// eslint-disable-next-line import/no-cycle
import { request } from './requestHandler.js';
import error from './error.js';

/**
* Returns the bot member from a guild.
* @param guild The guild to get the bot member from.
* @returns The bot member from the guild, or undefined if the guild is null or undefined.
* Retrieves the custom bot member from the given guild.
* @param guild - The guild to retrieve the custom bot member from.
* @returns A Promise that resolves with the custom bot member.
*/
export default async (guild: Discord.Guild | null | undefined) => {
if (!guild) return undefined;
export default async (guild: Discord.Guild) => {
const botId = await getBotIdFromGuild(guild);
const rawMember = await request.guilds.getMember(guild, botId);

return (
request.guilds
.getMember(guild, await getBotIdFromGuild(guild))
.then((m) => ('message' in m ? undefined : m)) ?? guild.members.me
);
if ('message' in rawMember) {
error(guild, new Error(rawMember.message));
return request.guilds.getMember(
undefined,
guild.client.user.id,
guild,
) as Promise<Discord.GuildMember>;
}

return rawMember;
};
25 changes: 0 additions & 25 deletions src/BaseClient/ClientHelperModules/getCustomBot.ts

This file was deleted.

12 changes: 8 additions & 4 deletions src/BaseClient/ClientHelperModules/getCustomCommand.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as Discord from 'discord.js';
import cache from './cache.js';
import SlashCommands from '../../Events/readyEvents/startupTasks/SlashCommands.js';

Expand All @@ -10,11 +11,14 @@ type CommandName = (typeof SlashCommands)['names'][number];
* @param name - The name of the command to retrieve.
* @returns The custom command with the specified name, or undefined if it does not exist.
*/
export default async (guildId: string | undefined | null, name: CommandName) => {
export default async (guild: Discord.Guild | undefined | null, name: CommandName) => {
const { default: client } = await import('../Client.js');

const clientCommand = client.application?.commands.cache.find((c) => c.name === name);
return guildId
? cache.commands.get(guildId)?.find((c) => c.name === name) ?? clientCommand
const clientCommand =
guild?.commands.cache.find((c) => c.name === name) ??
client.application?.commands.cache.find((c) => c.name === name);

return guild
? cache.commands.get(guild.id)?.find((c) => c.name === name) ?? clientCommand
: clientCommand;
};
26 changes: 13 additions & 13 deletions src/BaseClient/ClientHelperModules/mod/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import getStrike from './getStrike.js';
import err from './err.js';
import constants from '../../Other/constants.js';
import objectEmotes from '../emotes.js';
import getCustomBot from '../getCustomBot.js';
import getBotMemberFromGuild from '../getBotMemberFromGuild.js';
import isManageable from '../isManageable.js';
import isModeratable from '../isModeratable.js';
import DataBase from '../../DataBase.js';
Expand Down Expand Up @@ -81,7 +81,7 @@ const mod = {
return false;
}

const me = await getCustomBot(options.guild);
const me = await getBotMemberFromGuild(options.guild);
options.roles = options.roles.filter((r) => r.position < Number(me.roles.highest.position));

if ((!isManageable(targetMember, me) || !options.roles.length) && !options.skipChecks) {
Expand Down Expand Up @@ -159,7 +159,7 @@ const mod = {
return false;
}

const me = await getCustomBot(options.guild);
const me = await getBotMemberFromGuild(options.guild);
options.roles = options.roles.filter((r) => r.position < Number(me.roles.highest.position));

if (
Expand Down Expand Up @@ -207,7 +207,7 @@ const mod = {
}
const { targetMember } = memberResponse;

const me = await getCustomBot(options.guild);
const me = await getBotMemberFromGuild(options.guild);
if (!isModeratable(me, targetMember) && !options.skipChecks) {
permissionError(cmd, message, language, type);
return false;
Expand Down Expand Up @@ -243,7 +243,7 @@ const mod = {
);
m.default(undefined, 'muteRemove', {
dbOnly: false,
executor: (await getCustomBot(options.guild)).user,
executor: (await getBotMemberFromGuild(options.guild)).user,
guild: options.guild,
reason: language.mod.execution.muteRemove.reason,
target: options.target,
Expand Down Expand Up @@ -285,7 +285,7 @@ const mod = {
}
const { targetMember } = memberResponse;

const me = await getCustomBot(options.guild);
const me = await getBotMemberFromGuild(options.guild);
if (!isModeratable(me, targetMember) && !options.skipChecks) {
permissionError(cmd, message, language, type);
return false;
Expand Down Expand Up @@ -316,7 +316,7 @@ const mod = {
const type = 'banAdd';

const memberRes = await getMembers(cmd, options, language, message, type);
const me = await getCustomBot(options.guild);
const me = await getBotMemberFromGuild(options.guild);
if (
((memberRes && !isModeratable(me, memberRes.targetMember)) ||
!me.permissions.has(Discord.PermissionFlagsBits.BanMembers)) &&
Expand Down Expand Up @@ -377,7 +377,7 @@ const mod = {
);
m.default(undefined, 'banRemove', {
dbOnly: false,
executor: (await getCustomBot(options.guild)).user,
executor: (await getBotMemberFromGuild(options.guild)).user,
guild: options.guild,
reason: language.mod.execution.muteRemove.reason,
target: options.target,
Expand All @@ -400,7 +400,7 @@ const mod = {

cache.bans.delete(options.guild.id, options.target.id);

const me = await getCustomBot(options.guild);
const me = await getBotMemberFromGuild(options.guild);
if (!me.permissions.has(Discord.PermissionFlagsBits.BanMembers) && !options.skipChecks) {
permissionError(cmd, message, language, type);
return false;
Expand All @@ -427,7 +427,7 @@ const mod = {
) => {
const type = 'kickAdd';

const me = await getCustomBot(options.guild);
const me = await getBotMemberFromGuild(options.guild);
if (!me.permissions.has(Discord.PermissionFlagsBits.KickMembers) && !options.skipChecks) {
permissionError(cmd, message, language, type);
return false;
Expand Down Expand Up @@ -510,7 +510,7 @@ const mod = {
return true;
}

const me = await getCustomBot(options.guild);
const me = await getBotMemberFromGuild(options.guild);
if (
(!me.permissions.has(Discord.PermissionFlagsBits.ManageChannels) ||
!options.channel.permissionsFor(me).has(Discord.PermissionFlagsBits.ManageChannels)) &&
Expand Down Expand Up @@ -588,7 +588,7 @@ const mod = {
return true;
}

const me = await getCustomBot(options.guild);
const me = await getBotMemberFromGuild(options.guild);
if (
(!me.permissions.has(Discord.PermissionFlagsBits.ManageChannels) ||
!options.channel.permissionsFor(me).has(Discord.PermissionFlagsBits.ManageChannels)) &&
Expand Down Expand Up @@ -654,7 +654,7 @@ const mod = {
);
m.default(undefined, 'channelBanRemove', {
dbOnly: false,
executor: (await getCustomBot(options.guild)).user,
executor: (await getBotMemberFromGuild(options.guild)).user,
guild: options.guild,
reason: language.mod.execution.muteRemove.reason,
target: options.target,
Expand Down
6 changes: 3 additions & 3 deletions src/BaseClient/ClientHelperModules/requestHandler/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ const getGuildCommands = async (
return;
}

if (guild.client.application.commands.cache.get(p.id)) return;
guild.client.application.commands.cache.set(p.id, p);
if (guild.commands.cache.get(p.id)) return;
guild.commands.cache.set(p.id, p);
});
return parsed;
})
Expand Down Expand Up @@ -367,7 +367,7 @@ const bulkOverwriteGuildCommands = async (
return parsed;
}

parsed.forEach((p) => guild.client.application.commands.cache.set(p.id, p));
parsed.forEach((p) => guild.commands.cache.set(p.id, p));
return parsed;
})
.catch((e) => {
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/ButtonCommands/embed-builder/startOver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ export default async (
cmd.update({
embeds: [
{
color: ch.getColor(await ch.getBotMemberFromGuild(cmd.guild)),
color: ch.getColor(cmd.guild ? await ch.getBotMemberFromGuild(cmd.guild) : undefined),
description: `${lan.desc}\n\n${lan.oneRequired}\n${ch.constants.customembeds.needsOneOf
.map((n) => lan.embedProperties[n as keyof typeof lan.embedProperties])
.filter((a): a is string => !!a)
.join(', ')}`,
fields: lan
.fields((await ch.getCustomCommand(cmd.guildId, 'stp'))?.id ?? '0')
.fields((await ch.getCustomCommand(cmd.guild, 'stp'))?.id ?? '0')
.map((f) => ({ name: '\u200b', value: `${lan.quick}\n${f}` })),
},
embed,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Discord from 'discord.js';
import { getPayload } from '../../SlashCommands/check.js';
import * as ch from '../../../BaseClient/ClientHelper.js';
import { getPayload } from '../../../SlashCommands/mod/check.js';
import * as ch from '../../../../BaseClient/ClientHelper.js';

export default async (cmd: Discord.ButtonInteraction, args: string[]) => {
if (!cmd.inCachedGuild()) return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Discord from 'discord.js';
import * as ch from '../../../BaseClient/ClientHelper.js';
import * as ch from '../../../../BaseClient/ClientHelper.js';

export default async (cmd: Discord.ButtonInteraction, args: string[]) => {
if (!cmd.inCachedGuild()) return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Discord from 'discord.js';
import { getPayload } from '../../SlashCommands/check.js';
import * as ch from '../../../BaseClient/ClientHelper.js';
import { getPayload } from '../../../SlashCommands/mod/check.js';
import * as ch from '../../../../BaseClient/ClientHelper.js';

export default async (cmd: Discord.ButtonInteraction, args: string[]) => {
if (!cmd.inCachedGuild()) return;
Expand Down
21 changes: 6 additions & 15 deletions src/Commands/ButtonCommands/mod/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@ import * as Discord from 'discord.js';
import * as ch from '../../../BaseClient/ClientHelper.js';
import Commands from '../../../Events/readyEvents/startupTasks/SlashCommands.js';
import permissions from '../../SlashCommands/mod/permissions.js';
import * as CT from '../../../Typings/CustomTypings.js';

type CommandType =
| 'ban'
| 'strike'
| 'channel-ban'
| 'kick'
| 'soft-ban'
| 'temp-ban'
| 'temp-channel-ban'
| 'tempmute'
| 'unban'
| 'unmute'
| 'warn';
type CommandType = keyof CT.Language['slashCommands']['moderation']['permissions']['buttons'];

export default async (cmd: Discord.ButtonInteraction, args: CommandType[]) => {
if (!cmd.inCachedGuild()) return;
Expand All @@ -31,13 +21,14 @@ export default async (cmd: Discord.ButtonInteraction, args: CommandType[]) => {
message: cmd.message,
});

const name = args.shift();
const name = args.shift() as CommandType;
if (!name) {
ch.error(cmd.guild, new Error('No name provided'));
return;
}

const command = cmd.guild.commands.cache.find((c) => c.name === name);
const command = await ch.getCustomCommand(cmd.guild, name);

if (command) {
await ch.request.commands.deleteGuildCommand(cmd.guild, command.id);
permissions(cmd, [], response);
Expand All @@ -50,7 +41,7 @@ export default async (cmd: Discord.ButtonInteraction, args: CommandType[]) => {
return;
}

ch.request.commands.createGuildCommand(cmd.guild, submitCmdData);
await ch.request.commands.createGuildCommand(cmd.guild, submitCmdData);
permissions(cmd, [], response);
};

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ButtonCommands/suggestion/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default async (cmd: Discord.ButtonInteraction) => {
if (!settings?.active) {
ch.errorCmd(
cmd,
lan.cantBan((await ch.getCustomCommand(cmd.guildId, 'settings'))?.id ?? '0'),
lan.cantBan((await ch.getCustomCommand(cmd.guild, 'settings'))?.id ?? '0'),
language,
);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ModalCommands/check.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Discord from 'discord.js';
import * as ch from '../../BaseClient/ClientHelper.js';
import { getPayload } from '../SlashCommands/check.js';
import { getPayload } from '../SlashCommands/mod/check.js';

export default async (cmd: Discord.ModalSubmitInteraction, args: string[]) => {
if (!cmd.inCachedGuild()) return;
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/SelectCommands/StringSelect/check.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Discord from 'discord.js';
import * as ch from '../../../BaseClient/ClientHelper.js';
import { getPayload } from '../../SlashCommands/check.js';
import { getPayload } from '../../SlashCommands/mod/check.js';

export default async (cmd: Discord.StringSelectMenuInteraction, args: string[]) => {
if (!cmd.inCachedGuild()) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export default async (

embed.description =
type === 'reaction-roles'
? lan.descReactions((await ch.getCustomCommand(cmd.guildId, 'settings'))?.id ?? '0')
: lan.descButtons((await ch.getCustomCommand(cmd.guildId, 'settings'))?.id ?? '0');
? lan.descReactions((await ch.getCustomCommand(cmd.guild, 'settings'))?.id ?? '0')
: lan.descButtons((await ch.getCustomCommand(cmd.guild, 'settings'))?.id ?? '0');
embed.fields = [
...(embed.fields ?? []),
...(findField(value, embed.fields)
Expand Down
Loading

0 comments on commit 24814e0

Please sign in to comment.