Skip to content

Commit

Permalink
Fix some imports
Browse files Browse the repository at this point in the history
  • Loading branch information
alexemanuelol committed Jul 27, 2024
1 parent bcc0475 commit 2ead87d
Show file tree
Hide file tree
Showing 24 changed files with 174 additions and 205 deletions.
10 changes: 5 additions & 5 deletions src/discordEvents/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import { Message } from 'discord.js';

import { log } from '../../index';
import { getGuild, getTextChannel } from '../discordTools/discord-tools';
import * as discordTools from '../discordTools/discord-tools';
const { DiscordBot } = require('../structures/DiscordBot.js');
const DiscordCommandHandler = require('../handlers/discordCommandHandler.js');

Expand All @@ -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 getGuild(client, guildId);
const guild = await discordTools.getGuild(client, guildId);
if (!guild) return;
const channel = await getTextChannel(client, guild.id, message.channelId);
const channel = await discordTools.getTextChannel(client, 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 getGuild(client, guildId);
const guild = await discordTools.getGuild(client, guildId);
if (!guild) return;
const channel = await getTextChannel(client, guild.id, message.channelId);
const channel = await discordTools.getTextChannel(client, guild.id, message.channelId);
if (!channel) return;
log.info(client.intlGet(null, `logDiscordMessage`, {
guild: `${guild.name} (${guild.id})`,
Expand Down
4 changes: 2 additions & 2 deletions src/discordTools/discord-select-menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import * as path from 'path';
import * as guildInstance from '../util/guild-instance';
import { localeManager as lm } from "../../index";
import { languageCodes } from "../util/languages";
import * as Constants from '../util/constants';
import * as constants from '../util/constants';


export interface SelectMenuOptions {
Expand Down Expand Up @@ -53,7 +53,7 @@ export function getSelectMenu(guildId: string, options: SelectMenuOptions = {}):
option.description = lm.getIntl(instance.generalSettings.language, 'empty');
continue;
}
option.description = option.description.substring(0, Constants.SELECT_MENU_MAX_DESCRIPTION_CHARACTERS);
option.description = option.description.substring(0, constants.SELECT_MENU_MAX_DESCRIPTION_CHARACTERS);
}
selectMenu.setOptions(options.options);
}
Expand Down
53 changes: 27 additions & 26 deletions src/discordTools/discord-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/

import { Guild, Role, GuildMember, GuildBasedChannel, TextChannel, ChannelType, CategoryChannel, Message, PermissionFlagsBits } from 'discord.js';
import * as discordjs from 'discord.js';

import { log } 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<Guild | undefined> {
export async function getGuild(client: typeof DiscordBot, guildId: string): Promise<discordjs.Guild | undefined> {
try {
if (!Config.discord.useCache) {
await client.guilds.fetch();
Expand All @@ -39,10 +39,10 @@ export async function getGuild(client: typeof DiscordBot, guildId: string): Prom
}

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

if (guild instanceof Guild) {
if (guild instanceof discordjs.Guild) {
try {
if (!Config.discord.useCache) {
await guild.roles.fetch();
Expand All @@ -62,10 +62,10 @@ export async function getRole(client: typeof DiscordBot, guildId: string, role:
}

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

if (guild instanceof Guild) {
if (guild instanceof discordjs.Guild) {
try {
if (!Config.discord.useCache) {
await guild.members.fetch();
Expand All @@ -85,10 +85,10 @@ export async function getMember(client: typeof DiscordBot, guildId: string, memb
}

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

if (guild instanceof Guild) {
if (guild instanceof discordjs.Guild) {
try {
if (!Config.discord.useCache) {
await guild.channels.fetch();
Expand All @@ -108,31 +108,31 @@ async function getChannel(client: typeof DiscordBot, guildId: string, channel: s
}

export async function getTextChannel(client: typeof DiscordBot, guildId: string, channel: string,
isChannelId: boolean = true): Promise<TextChannel | undefined> {
isChannelId: boolean = true): Promise<discordjs.TextChannel | undefined> {
const ch = await getChannel(client, guildId, channel, isChannelId);
if (ch && ch.type === ChannelType.GuildText) {
return ch as TextChannel;
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,
isCategoryId: boolean = true): Promise<CategoryChannel | undefined> {
isCategoryId: boolean = true): Promise<discordjs.CategoryChannel | undefined> {
const categoryChannel = await getChannel(client, guildId, category, isCategoryId);
if (categoryChannel && categoryChannel.type === ChannelType.GuildCategory) {
return categoryChannel as CategoryChannel;
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):
Promise<Message | undefined> {
Promise<discordjs.Message | undefined> {
const guild = await getGuild(client, guildId);

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

if (channel instanceof TextChannel) {
if (channel instanceof discordjs.TextChannel) {
try {
/* Fetching the specific messageId because otherwise it only fetches last 50 messages. */
await channel.messages.fetch(messageId);
Expand All @@ -148,7 +148,7 @@ export async function getMessage(client: typeof DiscordBot, guildId: string, cha
export async function deleteMessage(client: typeof DiscordBot, guildId: string, channelId: string, messageId: string):
Promise<boolean> {
const message = await getMessage(client, guildId, channelId, messageId);
if (message instanceof Message) {
if (message instanceof discordjs.Message) {
try {
await message.delete();
return true;
Expand All @@ -160,24 +160,25 @@ export async function deleteMessage(client: typeof DiscordBot, guildId: string,
}

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

if (guild instanceof Guild) {
if (guild instanceof discordjs.Guild) {
try {
const channel = await guild.channels.create({
name: name,
type: type,
permissionOverwrites: [{
id: guild.roles.everyone.id,
deny: [PermissionFlagsBits.SendMessages]
deny: [discordjs.PermissionFlagsBits.SendMessages]
}]
});

if (type === ChannelType.GuildCategory) {
return channel as CategoryChannel;
} else if (type === ChannelType.GuildText) {
return channel as TextChannel;
if (type === discordjs.ChannelType.GuildCategory) {
return channel as discordjs.CategoryChannel;
} else if (type === discordjs.ChannelType.GuildText) {
return channel as discordjs.TextChannel;
}
}
catch (e) { }
Expand All @@ -204,7 +205,7 @@ export async function clearTextChannel(client: typeof DiscordBot, guildId: strin
numberOfMessages: number): Promise<boolean> {
const channel = await getTextChannel(client, guildId, channelId);

if (channel instanceof TextChannel) {
if (channel instanceof discordjs.TextChannel) {
try {
const messages = await channel.messages.fetch({ limit: numberOfMessages });
await channel.bulkDelete(messages, true);
Expand Down
12 changes: 6 additions & 6 deletions src/discordTools/discordEmbeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

const Discord = require('discord.js');

import { getMember } from './discord-tools';
import * as discordTools from './discord-tools';
const Client = require('../../index.ts');
const Constants = require('../util/constants.ts');
const Credentials = require('../util/credentials.ts');
Expand Down Expand Up @@ -70,7 +70,7 @@ module.exports = {
const server = instance.serverList[serverId];
let hoster = Client.client.intlGet(guildId, 'unknown');
if (credentials.hasOwnProperty(server.steamId)) {
hoster = await getMember(Client.client, guildId, credentials[server.steamId].discordUserId);
hoster = await discordTools.getMember(Client.client, guildId, credentials[server.steamId].discordUserId);
hoster = hoster.user.username;
}

Expand Down Expand Up @@ -498,7 +498,7 @@ module.exports = {
const server = instance.serverList[serverId];
const entity = server.storageMonitors[entityId];
const credentials = Credentials.readCredentialsFile();
const user = await getMember(Client.client, guildId, credentials[server.steamId].discordUserId);
const user = await discordTools.getMember(Client.client, guildId, credentials[server.steamId].discordUserId);
const grid = entity.location !== null ? ` (${entity.location})` : '';

return module.exports.getEmbed({
Expand All @@ -519,7 +519,7 @@ module.exports = {
const server = instance.serverList[serverId];
const entity = instance.serverList[serverId].switches[entityId];
const credentials = Credentials.readCredentialsFile();
const user = await getMember(Client.client, guildId, credentials[server.steamId].discordUserId);
const user = await discordTools.getMember(Client.client, guildId, credentials[server.steamId].discordUserId);
const grid = entity.location !== null ? ` (${entity.location})` : '';

return module.exports.getEmbed({
Expand All @@ -540,7 +540,7 @@ module.exports = {
const server = instance.serverList[serverId];
const entity = server.alarms[entityId];
const credentials = Credentials.readCredentialsFile();
const user = await getMember(Client.client, guildId, credentials[server.steamId].discordUserId);
const user = await discordTools.getMember(Client.client, guildId, credentials[server.steamId].discordUserId);
const grid = entity.location !== null ? ` (${entity.location})` : '';

return module.exports.getEmbed({
Expand Down Expand Up @@ -990,7 +990,7 @@ module.exports = {
let hoster = '';

for (const credential in credentials) {
const user = await getMember(Client.client, guildId, credentials[credential].discordUserId);
const user = await discordTools.getMember(Client.client, guildId, credentials[credential].discordUserId);
names += `${user.user.username}\n`;
steamIds += `${credential}\n`;
hoster += `${credential === instance.hoster ? `${Constants.LEADER_EMOJI}\n` : '\u200B\n'}`;
Expand Down
40 changes: 15 additions & 25 deletions src/discordTools/discordMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,9 @@ const Discord = require('discord.js');
const Path = require('path');

import { log } from '../../index';
import { getTextChannel, getMessage } from './discord-tools';
import { getSmartSwitchSelectMenu } from './discord-select-menus';
import {
getServerButtons,
getSmartSwitchButtons,
getSmartSwitchGroupButtons,
getSmartAlarmButtons,
getStorageMonitorToolCupboardButtons,
getStorageMonitorContainerButton,
getRecycleDeleteButton,
getTrackerButtons,
getHelpButtons
} from './discord-buttons';
import * as discordTools from './discord-tools';
import * as discordSelectMenus from './discord-select-menus';
import * as discordButtons from './discord-buttons';
const Constants = require('../util/constants.ts');
const Client = require('../../index.ts');
const DiscordEmbeds = require('./discordEmbeds.js');
Expand All @@ -48,13 +38,13 @@ module.exports = {
}

let message = messageId !== null ?
await getMessage(Client.client, guildId, channelId, messageId) : undefined;
await discordTools.getMessage(Client.client, guildId, channelId, messageId) : undefined;

if (message !== undefined) {
return await Client.client.messageEdit(message, content);
}
else {
const channel = await getTextChannel(Client.client, guildId, channelId);
const channel = await discordTools.getTextChannel(Client.client, guildId, channelId);

if (!channel) {
log.error(Client.client.intlGet(null, 'couldNotGetChannelWithId', { id: channelId }));
Expand All @@ -71,7 +61,7 @@ module.exports = {

const content = {
embeds: [await DiscordEmbeds.getServerEmbed(guildId, serverId)],
components: getServerButtons(Client.client, guildId, serverId, state)
components: discordButtons.getServerButtons(Client.client, guildId, serverId, state)
}

const message = await module.exports.sendMessage(guildId, content, server.messageId,
Expand All @@ -89,7 +79,7 @@ module.exports = {

const content = {
embeds: [DiscordEmbeds.getTrackerEmbed(guildId, trackerId)],
components: getTrackerButtons(guildId, trackerId)
components: discordButtons.getTrackerButtons(guildId, trackerId)
}

const message = await module.exports.sendMessage(guildId, content, tracker.messageId,
Expand All @@ -110,8 +100,8 @@ module.exports = {
DiscordEmbeds.getSmartSwitchEmbed(guildId, serverId, entityId) :
DiscordEmbeds.getNotFoundSmartDeviceEmbed(guildId, serverId, entityId, 'switches')],
components: [
getSmartSwitchSelectMenu(guildId, serverId, entityId),
getSmartSwitchButtons(guildId, serverId, entityId)
discordSelectMenus.getSmartSwitchSelectMenu(guildId, serverId, entityId),
discordButtons.getSmartSwitchButtons(guildId, serverId, entityId)
],
files: [
new Discord.AttachmentBuilder(Path.join(__dirname, '..', `resources/images/electrics/${entity.image}`))
Expand All @@ -135,7 +125,7 @@ module.exports = {
embeds: [entity.reachable ?
DiscordEmbeds.getSmartAlarmEmbed(guildId, serverId, entityId) :
DiscordEmbeds.getNotFoundSmartDeviceEmbed(guildId, serverId, entityId, 'alarms')],
components: [getSmartAlarmButtons(guildId, serverId, entityId)],
components: [discordButtons.getSmartAlarmButtons(guildId, serverId, entityId)],
files: [new Discord.AttachmentBuilder(
Path.join(__dirname, '..', `resources/images/electrics/${entity.image}`))]
}
Expand All @@ -158,8 +148,8 @@ module.exports = {
DiscordEmbeds.getStorageMonitorEmbed(guildId, serverId, entityId) :
DiscordEmbeds.getNotFoundSmartDeviceEmbed(guildId, serverId, entityId, 'storageMonitors')],
components: [entity.type === 'toolCupboard' ?
getStorageMonitorToolCupboardButtons(guildId, serverId, entityId) :
getStorageMonitorContainerButton(guildId, serverId, entityId)],
discordButtons.getStorageMonitorToolCupboardButtons(guildId, serverId, entityId) :
discordButtons.getStorageMonitorContainerButton(guildId, serverId, entityId)],
files: [
new Discord.AttachmentBuilder(
Path.join(__dirname, '..', `resources/images/electrics/${entity.image}`))]
Expand All @@ -182,7 +172,7 @@ module.exports = {

const content = {
embeds: [DiscordEmbeds.getSmartSwitchGroupEmbed(guildId, serverId, groupId)],
components: getSmartSwitchGroupButtons(guildId, serverId, groupId),
components: discordButtons.getSmartSwitchGroupButtons(guildId, serverId, groupId),
files: [new Discord.AttachmentBuilder(
Path.join(__dirname, '..', `resources/images/electrics/${group.image}`))]
}
Expand All @@ -201,7 +191,7 @@ module.exports = {

const content = {
embeds: [DiscordEmbeds.getStorageMonitorRecycleEmbed(guildId, serverId, entityId, items)],
components: [getRecycleDeleteButton()],
components: [discordButtons.getRecycleDeleteButton()],
files: [new Discord.AttachmentBuilder(
Path.join(__dirname, '..', 'resources/images/electrics/recycler.png'))]
}
Expand Down Expand Up @@ -527,7 +517,7 @@ module.exports = {
sendHelpMessage: async function (interaction) {
const content = {
embeds: [DiscordEmbeds.getHelpEmbed(interaction.guildId)],
components: getHelpButtons(),
components: discordButtons.getHelpButtons(),
ephemeral: true
}

Expand Down
6 changes: 3 additions & 3 deletions src/discordTools/remove-guild-channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import { Guild } from 'discord.js';

import { deleteChannel } from './discord-tools';
import * as discordTools from './discord-tools';
const { DiscordBot } = require('../structures/DiscordBot.js');

export async function removeGuildChannels(client: typeof DiscordBot, guild: Guild) {
Expand All @@ -34,11 +34,11 @@ export async function removeGuildChannels(client: typeof DiscordBot, guild: Guil
continue;
}

await deleteChannel(client, guildId, channelId as string);
await discordTools.deleteChannel(client, guildId, channelId as string);
instance.channelIds[channelName] = null;
}

await deleteChannel(client, guildId, categoryId as string);
await discordTools.deleteChannel(client, guildId, categoryId as string);

instance.channelIds['category'] = null;
client.setInstance(guildId, instance);
Expand Down
Loading

0 comments on commit 2ead87d

Please sign in to comment.