Skip to content

Commit

Permalink
Use client directly in discordTools instead of passing as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
alexemanuelol committed Jul 31, 2024
1 parent d762f4b commit 4e5b917
Show file tree
Hide file tree
Showing 27 changed files with 129 additions and 98 deletions.
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ createMissingDirectories();
export const log = Logger.createLogger('logs/rustplusplus.log');
export const localeManager = new LocaleManager(Config.general.language);

const client = new DiscordBot({
export const client = new DiscordBot({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/blacklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = {
return;
}

const guild = await discordToolsgetGuild(client, guildId);
const guild = await discordTools.getGuild(guildId);

switch (interaction.options.getSubcommand()) {
case 'add': {
Expand Down Expand Up @@ -216,7 +216,7 @@ module.exports = {
let steamIds = '';

for (const discordId of instance.blacklist['discordIds']) {
const user = await discordToolsgetMember(client, guildId, discordId)
const user = await discordTools.getMember(guildId, discordId)
let name = '';
if (user) name = `${user.user.username} (${user.id})`;
else name = `${discordId}`;
Expand Down
10 changes: 5 additions & 5 deletions src/commands/credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ async function addCredentials(client, interaction, verifyId) {

/* Start Fcm Listener */
if (isHoster) {
require('../util/FcmListener')(client, await discordTools.getGuild(client, interaction.guildId));
require('../util/FcmListener')(client, await discordTools.getGuild(interaction.guildId));
if (prevHoster !== null) {
require('../util/FcmListenerLite')(client, await discordTools.getGuild(client, interaction.guildId), prevHoster);
require('../util/FcmListenerLite')(client, await discordTools.getGuild(interaction.guildId), prevHoster);
}
}
else {
require('../util/FcmListenerLite')(client, await discordTools.getGuild(client, interaction.guildId), steamId);
require('../util/FcmListenerLite')(client, await discordTools.getGuild(interaction.guildId), steamId);

const rustplus = client.rustplusInstances[guildId];
if (rustplus && rustplus.team.leaderSteamId === steamId) {
Expand Down Expand Up @@ -329,9 +329,9 @@ async function setHosterCredentials(client, interaction, verifyId) {
await DiscordMessages.sendServerMessage(guildId, rustplus.serverId);
}

require('../util/FcmListener')(client, await discordTools.getGuild(client, interaction.guildId));
require('../util/FcmListener')(client, await discordTools.getGuild(interaction.guildId));
if (prevHoster !== null) {
require('../util/FcmListenerLite')(client, await discordTools.getGuild(client, interaction.guildId), prevHoster);
require('../util/FcmListenerLite')(client, await discordTools.getGuild(interaction.guildId), prevHoster);
}

log.info(client.intlGet(null, 'slashCommandValueChange', {
Expand Down
28 changes: 14 additions & 14 deletions src/commands/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module.exports = {
return;
}

const guild = await discordTools.getGuild(client, interaction.guildId);
const guild = await discordTools.getGuild(interaction.guildId);

switch (interaction.options.getSubcommand()) {
case 'discord': {
Expand All @@ -103,10 +103,10 @@ module.exports = {
/* Ignore */
}

await discordTools.clearTextChannel(client, guild.id, instance.channelIds.information, 100);
await discordTools.clearTextChannel(client, guild.id, instance.channelIds.switches, 100);
await discordTools.clearTextChannel(client, guild.id, instance.channelIds.switchGroups, 100);
await discordTools.clearTextChannel(client, guild.id, instance.channelIds.storageMonitors, 100);
await discordTools.clearTextChannel(guild.id, instance.channelIds.information, 100);
await discordTools.clearTextChannel(guild.id, instance.channelIds.switches, 100);
await discordTools.clearTextChannel(guild.id, instance.channelIds.switchGroups, 100);
await discordTools.clearTextChannel(guild.id, instance.channelIds.storageMonitors, 100);

const rustplus = client.rustplusInstances[guild.id];
if (rustplus && rustplus.isOperational) {
Expand Down Expand Up @@ -135,7 +135,7 @@ module.exports = {
} break;

case 'information': {
await discordTools.clearTextChannel(client, guild.id, instance.channelIds.information, 100);
await discordTools.clearTextChannel(guild.id, instance.channelIds.information, 100);

const rustplus = client.rustplusInstances[guild.id];
if (rustplus && rustplus.isOperational) {
Expand All @@ -152,7 +152,7 @@ module.exports = {
case 'servers': {
const perms = PermissionHandler.getPermissionsRemoved(client, guild);
try {
const category = await discordTools.getCategory(client, guild.id, instance.channelIds.category);
const category = await discordTools.getCategory(guild.id, instance.channelIds.category);
await category.permissionOverwrites.set(perms);
}
catch (e) {
Expand All @@ -172,7 +172,7 @@ module.exports = {
case 'settings': {
const perms = PermissionHandler.getPermissionsRemoved(client, guild);
try {
const category = await discordTools.getCategory(client, guild.id, instance.channelIds.category);
const category = await discordTools.getCategory(guild.id, instance.channelIds.category);
await category.permissionOverwrites.set(perms);
}
catch (e) {
Expand All @@ -190,12 +190,12 @@ module.exports = {
} break;

case 'switches': {
await discordTools.clearTextChannel(client, guild.id, instance.channelIds.switches, 100);
await discordTools.clearTextChannel(client, guild.id, instance.channelIds.switchGroups, 100);
await discordTools.clearTextChannel(guild.id, instance.channelIds.switches, 100);
await discordTools.clearTextChannel(guild.id, instance.channelIds.switchGroups, 100);

const perms = PermissionHandler.getPermissionsRemoved(client, guild);
try {
const category = await discordTools.getCategory(client, guild.id, instance.channelIds.category);
const category = await discordTools.getCategory(guild.id, instance.channelIds.category);
await category.permissionOverwrites.set(perms);
}
catch (e) {
Expand Down Expand Up @@ -229,11 +229,11 @@ module.exports = {
} break;

case 'storagemonitors': {
await discordTools.clearTextChannel(client, guild.id, instance.channelIds.storageMonitors, 100);
await discordTools.clearTextChannel(guild.id, instance.channelIds.storageMonitors, 100);

const perms = PermissionHandler.getPermissionsRemoved(client, guild);
try {
const category = await discordTools.getCategory(client, guild.id, instance.channelIds.category);
const category = await discordTools.getCategory(guild.id, instance.channelIds.category);
await category.permissionOverwrites.set(perms);
}
catch (e) {
Expand All @@ -256,7 +256,7 @@ module.exports = {
case 'trackers': {
const perms = PermissionHandler.getPermissionsRemoved(client, guild);
try {
const category = await discordTools.getCategory(client, guild.id, instance.channelIds.category);
const category = await discordTools.getCategory(guild.id, instance.channelIds.category);
await category.permissionOverwrites.set(perms);
}
catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module.exports = {
value: `${interaction.options.getSubcommand()}`
}));

const guild = await discordTools.getGuild(client, interaction.guildId);
const guild = await discordTools.getGuild(interaction.guildId);
if (guild) {
const category = await setupGuildCategory(client, guild);
await setupGuildChannels(client, guild, category);
Expand Down
8 changes: 4 additions & 4 deletions src/discordEvents/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export async function execute(client: typeof DiscordBot, message: Message) {

if (instance.blacklist['discordIds'].includes(message.author.id) &&
Object.values(instance.channelIds).includes(message.channelId)) {
const guild = await discordTools.getGuild(client, guildId);
const guild = await discordTools.getGuild(guildId);
if (!guild) return;
const channel = await discordTools.getTextChannel(client, guild.id, message.channelId);
const channel = await discordTools.getTextChannel(guild.id, message.channelId);
if (!channel) return;
log.info(client.intlGet(null, `userPartOfBlacklistDiscord`, {
guild: `${guild.name} (${guild.id})`,
Expand All @@ -55,9 +55,9 @@ export async function execute(client: typeof DiscordBot, message: Message) {
await DiscordCommandHandler.discordCommandHandler(rustplus, client, message);
}
else if (message.channelId === instance.channelIds.teamchat) {
const guild = await discordTools.getGuild(client, guildId);
const guild = await discordTools.getGuild(guildId);
if (!guild) return;
const channel = await discordTools.getTextChannel(client, guild.id, message.channelId);
const channel = await discordTools.getTextChannel(guild.id, message.channelId);
if (!channel) return;
log.info(client.intlGet(null, `logDiscordMessage`, {
guild: `${guild.name} (${guild.id})`,
Expand Down
10 changes: 5 additions & 5 deletions src/discordTools/discord-embeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export async function getServerEmbed(client: typeof DiscordBot, guildId: string,

let hoster = lm.getIntl(language, 'unknown');
if (creds.hasOwnProperty(server.steamId)) {
const member = await discordTools.getMember(client, guildId, creds[server.steamId].discordUserId);
const member = await discordTools.getMember(guildId, creds[server.steamId].discordUserId);
if (member !== undefined) {
hoster = member.user.username;
}
Expand Down Expand Up @@ -555,7 +555,7 @@ export async function getStorageMonitorNotFoundEmbed(client: typeof DiscordBot,
const server = instance.serverList[serverId];
const entity = server.storageMonitors[entityId];
const creds = credentials.readCredentialsFile();
const user = await discordTools.getMember(client, guildId, creds[server.steamId].discordUserId);
const user = await discordTools.getMember(guildId, creds[server.steamId].discordUserId);
const grid = entity.location !== null ? ` (${entity.location})` : '';

return getEmbed({
Expand All @@ -578,7 +578,7 @@ export async function getSmartSwitchNotFoundEmbed(client: typeof DiscordBot, gui
const server = instance.serverList[serverId];
const entity = instance.serverList[serverId].switches[entityId];
const creds = credentials.readCredentialsFile();
const user = await discordTools.getMember(client, guildId, creds[server.steamId].discordUserId);
const user = await discordTools.getMember(guildId, creds[server.steamId].discordUserId);
const grid = entity.location !== null ? ` (${entity.location})` : '';

return getEmbed({
Expand All @@ -601,7 +601,7 @@ export async function getSmartAlarmNotFoundEmbed(client: typeof DiscordBot, guil
const server = instance.serverList[serverId];
const entity = server.alarms[entityId];
const creds = credentials.readCredentialsFile();
const user = await discordTools.getMember(client, guildId, creds[server.steamId].discordUserId);
const user = await discordTools.getMember(guildId, creds[server.steamId].discordUserId);
const grid = entity.location !== null ? ` (${entity.location})` : '';

return getEmbed({
Expand Down Expand Up @@ -1073,7 +1073,7 @@ export async function getCredentialsShowEmbed(client: typeof DiscordBot, guildId
let hoster = '';

for (const credential in creds) {
const user = await discordTools.getMember(client, guildId, creds[credential].discordUserId);
const user = await discordTools.getMember(guildId, creds[credential].discordUserId);
names += `${user !== undefined ? user.user.username : lm.getIntl(language, 'unknown')}\n`;
steamIds += `${credential}\n`;
hoster += `${credential === instance.hoster ? `${constants.LEADER_EMOJI}\n` : '\u200B\n'}`;
Expand Down
79 changes: 55 additions & 24 deletions src/discordTools/discord-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

import * as discordjs from 'discord.js';

import { log } from '../../index';
import { log, client } from '../../index';
import { localeManager as lm } from '../../index';
const Config = require('../../config');
const { DiscordBot } = require('../structures/DiscordBot.js');

export async function getGuild(client: typeof DiscordBot, guildId: string): Promise<discordjs.Guild | undefined> {
export async function getGuild(guildId: string): Promise<discordjs.Guild | undefined> {
try {
if (!Config.discord.useCache) {
await client.guilds.fetch();
Expand All @@ -38,9 +38,9 @@ export async function getGuild(client: typeof DiscordBot, guildId: string): Prom
return undefined;
}

export async function getRole(client: typeof DiscordBot, guildId: string, role: string, isRoleId: boolean = true):
export async function getRole(guildId: string, role: string, isRoleId: boolean = true):
Promise<discordjs.Role | undefined> {
const guild = await getGuild(client, guildId);
const guild = await getGuild(guildId);

if (guild instanceof discordjs.Guild) {
try {
Expand All @@ -61,9 +61,9 @@ export async function getRole(client: typeof DiscordBot, guildId: string, role:
return undefined;
}

export async function getMember(client: typeof DiscordBot, guildId: string, member: string, isMemberId: boolean = true):
export async function getMember(guildId: string, member: string, isMemberId: boolean = true):
Promise<discordjs.GuildMember | undefined> {
const guild = await getGuild(client, guildId);
const guild = await getGuild(guildId);

if (guild instanceof discordjs.Guild) {
try {
Expand All @@ -84,9 +84,9 @@ export async function getMember(client: typeof DiscordBot, guildId: string, memb
return undefined;
}

async function getChannel(client: typeof DiscordBot, guildId: string, channel: string, isChannelId: boolean = true):
async function getChannel(guildId: string, channel: string, isChannelId: boolean = true):
Promise<discordjs.GuildBasedChannel | undefined> {
const guild = await getGuild(client, guildId);
const guild = await getGuild(guildId);

if (guild instanceof discordjs.Guild) {
try {
Expand All @@ -107,30 +107,30 @@ async function getChannel(client: typeof DiscordBot, guildId: string, channel: s
return undefined;
}

export async function getTextChannel(client: typeof DiscordBot, guildId: string, channel: string,
export async function getTextChannel(guildId: string, channel: string,
isChannelId: boolean = true): Promise<discordjs.TextChannel | undefined> {
const ch = await getChannel(client, guildId, channel, isChannelId);
const ch = await getChannel(guildId, channel, isChannelId);
if (ch && ch.type === discordjs.ChannelType.GuildText) {
return ch as discordjs.TextChannel;
}
return undefined;
}

export async function getCategory(client: typeof DiscordBot, guildId: string, category: string,
export async function getCategory(guildId: string, category: string,
isCategoryId: boolean = true): Promise<discordjs.CategoryChannel | undefined> {
const categoryChannel = await getChannel(client, guildId, category, isCategoryId);
const categoryChannel = await getChannel(guildId, category, isCategoryId);
if (categoryChannel && categoryChannel.type === discordjs.ChannelType.GuildCategory) {
return categoryChannel as discordjs.CategoryChannel;
}
return undefined;
}

export async function getMessage(client: typeof DiscordBot, guildId: string, channelId: string, messageId: string):
export async function getMessage(guildId: string, channelId: string, messageId: string):
Promise<discordjs.Message | undefined> {
const guild = await getGuild(client, guildId);
const guild = await getGuild(guildId);

if (guild instanceof discordjs.Guild) {
const channel = await getTextChannel(client, guildId, channelId);
const channel = await getTextChannel(guildId, channelId);

if (channel instanceof discordjs.TextChannel) {
try {
Expand All @@ -145,9 +145,8 @@ export async function getMessage(client: typeof DiscordBot, guildId: string, cha
return undefined;
}

export async function deleteMessage(client: typeof DiscordBot, guildId: string, channelId: string, messageId: string):
Promise<boolean> {
const message = await getMessage(client, guildId, channelId, messageId);
export async function deleteMessage(guildId: string, channelId: string, messageId: string): Promise<boolean> {
const message = await getMessage(guildId, channelId, messageId);
if (message instanceof discordjs.Message) {
try {
await message.delete();
Expand All @@ -159,10 +158,10 @@ export async function deleteMessage(client: typeof DiscordBot, guildId: string,
return false;
}

export async function createChannel(client: typeof DiscordBot, guildId: string, name: string,
export async function createChannel(guildId: string, name: string,
type: discordjs.ChannelType.GuildCategory | discordjs.ChannelType.GuildText):
Promise<discordjs.CategoryChannel | discordjs.TextChannel | undefined> {
const guild = await getGuild(client, guildId);
const guild = await getGuild(guildId);

if (guild instanceof discordjs.Guild) {
try {
Expand All @@ -187,9 +186,9 @@ export async function createChannel(client: typeof DiscordBot, guildId: string,
return undefined;
}

export async function deleteChannel(client: typeof DiscordBot, guildId: string, channel: string,
export async function deleteChannel(guildId: string, channel: string,
isChannelId: boolean = true): Promise<boolean> {
const ch = await getChannel(client, guildId, channel, isChannelId);
const ch = await getChannel(guildId, channel, isChannelId);
if (ch) {
try {
await ch.delete();
Expand All @@ -201,9 +200,9 @@ export async function deleteChannel(client: typeof DiscordBot, guildId: string,
return false;
}

export async function clearTextChannel(client: typeof DiscordBot, guildId: string, channelId: string,
export async function clearTextChannel(guildId: string, channelId: string,
numberOfMessages: number): Promise<boolean> {
const channel = await getTextChannel(client, guildId, channelId);
const channel = await getTextChannel(guildId, channelId);

if (channel instanceof discordjs.TextChannel) {
try {
Expand Down Expand Up @@ -311,4 +310,36 @@ export async function messageEdit(message: discordjs.Message, content: discordjs
}

return undefined;
}

/* Only used for messages/interactions to a guild text channel message, not member PM. */
export async function sendUpdateMessage(guildId: string, content: discordjs.MessageCreateOptions |
discordjs.MessageEditOptions, channelId: string | null, messageId: string | null, interaction: discordjs.Interaction | null = null):
Promise<discordjs.Message | undefined> {
if (interaction !== null) {
await interactionUpdate(interaction, content as discordjs.InteractionUpdateOptions);
return undefined;
}

if (channelId === null) {
return undefined;
}

const message = messageId !== null ? await getMessage(guildId, channelId, messageId) : undefined;
if (message !== undefined) {
/* Message already exist, so update it. */
return await messageEdit(message, content as discordjs.MessageEditOptions);
}
else {
/* Message does not exist, so create it. */
const channel = await getTextChannel(guildId, channelId);

if (channel instanceof discordjs.TextChannel) {
return await messageSend(channel, content as discordjs.MessageCreateOptions);
}
else {
log.error(lm.getIntl(Config.general.language, 'couldNotGetChannelWithId', { id: channelId }));
return undefined;
}
}
}
Loading

0 comments on commit 4e5b917

Please sign in to comment.