diff --git a/config/options.json b/config/options.json index 35a64db..ee2bd29 100644 --- a/config/options.json +++ b/config/options.json @@ -1,5 +1,6 @@ { "color": "#eeaadb", + "guildid": "481741000365178881", "devMessage": "Developed by taken.lua", "applicationsChannel": "776705352456470550", "staffApplicationsChannel": "1039258641393520700", diff --git a/src/events/server/guildMemberAdd/logNewJoins.ts b/src/events/server/guildMemberAdd/logNewJoins.ts index 10d9af2..5642e50 100644 --- a/src/events/server/guildMemberAdd/logNewJoins.ts +++ b/src/events/server/guildMemberAdd/logNewJoins.ts @@ -1,6 +1,7 @@ -import { ChannelType, GuildMember, userMention } from "discord.js" -import { color, botLogChannel } from "../../../../config/options.json" +import { GuildMember, userMention } from "discord.js" +import { color } from "../../../../config/options.json" import { Event } from "../../../interfaces" +import logToChannel from "../../../utils/functions/logtochannel" export = { name: "logNewJoins", @@ -9,24 +10,9 @@ export = { event: "guildMemberAdd", execute(member: GuildMember) { - const channel = member.guild.channels.cache.get(botLogChannel) const embedColor = Number(color.replace("#", "0x")) - if (!channel) { - console.log( - "[ERROR] Could not find channel used for new join logging.", - ) - return - } - - if (channel.type !== ChannelType.GuildText) { - console.log( - "[ERROR] The channel used for new join logging is not a text channel.", - ) - return - } - - channel.send({ + logToChannel("bot", { embeds: [ { title: "New Member", diff --git a/src/events/server/ready/sendOnlineMessage.ts b/src/events/server/ready/sendOnlineMessage.ts index c7ec0fd..40dd0c8 100644 --- a/src/events/server/ready/sendOnlineMessage.ts +++ b/src/events/server/ready/sendOnlineMessage.ts @@ -1,7 +1,6 @@ -import { onlineLogChannel, color } from "../../../../config/options.json" +import logToChannel from "../../../utils/functions/logtochannel" import { Event } from "../../../interfaces" -import { ExtendedClient as Client } from "../../../utils/Client" -import { ChannelType } from "discord.js" +import { color } from "../../../../config/options.json" export = { name: "sendonlinemessage", @@ -9,25 +8,12 @@ export = { type: "event", event: "ready", - execute(client: Client) { + execute() { if (process.env.NODE_ENV === "dev") return - const channel = client.channels.cache.get(onlineLogChannel) const embedColor = Number(color.replace("#", "0x")) - if (!channel) { - console.log( - "[ERROR] Could not find channel used for online message.", - ) - return - } - - if (channel.type !== ChannelType.GuildText) { - console.log("[ERROR] Online message channel is not a text channel.") - return - } - - channel.send({ + logToChannel("online", { embeds: [ { description: "Bot is online!", diff --git a/src/events/server/voiceStateUpdate/vcJoinLeave.ts b/src/events/server/voiceStateUpdate/vcJoinLeave.ts index 9e8d4c2..530dc6c 100644 --- a/src/events/server/voiceStateUpdate/vcJoinLeave.ts +++ b/src/events/server/voiceStateUpdate/vcJoinLeave.ts @@ -2,10 +2,10 @@ import { userMention, channelMention, VoiceState, - ChannelType, } from "discord.js" -import { botLogChannel, color } from "../../../../config/options.json" +import { color } from "../../../../config/options.json" import { Event } from "../../../interfaces" +import logToChannel from "../../../utils/functions/logtochannel" export = { name: "vcJoinLeave", @@ -16,29 +16,12 @@ export = { execute(oldState: VoiceState, newState: VoiceState) { if (process.env.NODE_ENV === "dev") return - const guild = oldState.guild - const channel = guild.channels.cache.get(botLogChannel) const embedColor = Number(color.replace("#", "0x")) - - if (!channel) { - console.log( - "[ERROR] Could not find channel used for voice channel join/leave logging.", - ) - return - } - - if (channel.type !== ChannelType.GuildText) { - console.log( - "[ERROR] The channel used for voice channel join/leave logging is not a text channel.", - ) - return - } - const oldChannel = oldState.channel const newChannel = newState.channel if (oldChannel === null && newChannel !== null) { - channel.send({ + logToChannel("bot", { embeds: [ { title: "Voice Channel Join", @@ -55,7 +38,7 @@ export = { ], }) } else if (oldChannel !== null && newChannel === null) { - channel.send({ + logToChannel("bot", { embeds: [ { title: "Voice Channel Leave", @@ -74,7 +57,7 @@ export = { } else if (oldChannel !== null && newChannel !== null) { if (oldChannel.id === newChannel.id) return - channel.send({ + logToChannel("bot", { embeds: [ { title: "Voice Channel Switch", diff --git a/src/utils/eventHandlers/autocomplete.ts b/src/utils/eventHandlers/autocomplete.ts index 507b216..834a35e 100644 --- a/src/utils/eventHandlers/autocomplete.ts +++ b/src/utils/eventHandlers/autocomplete.ts @@ -1,9 +1,10 @@ import { ExtendedClient as Client } from "../Client" -import { errorLogChannel, color } from "../../../config/options.json" +import { color } from "../../../config/options.json" import { Autocomplete } from "../../interfaces" -import { Events, TextChannel } from "discord.js" +import { Events } from "discord.js" import path = require("path") import fs = require("fs") +import logToChannel from "../functions/logtochannel" type FileType = "js" | "ts" const embedColor = Number(color.replace("#", "0x")) @@ -52,14 +53,8 @@ export default function loadAutocompleteEvents(client: Client, ft: FileType) { await autocomplete.execute(interaction) } catch (error) { if (process.env.NODE_ENV !== "dev") { - const channel = client.channels.cache.get( - errorLogChannel, - ) as TextChannel - if (!channel) { - console.log("No error log channel found.") - } - await channel.send({ + await logToChannel("error", { embeds: [ { title: "Autocomplete error occured", diff --git a/src/utils/eventHandlers/button.ts b/src/utils/eventHandlers/button.ts index 2911787..25d6917 100644 --- a/src/utils/eventHandlers/button.ts +++ b/src/utils/eventHandlers/button.ts @@ -1,9 +1,10 @@ import { ExtendedClient as Client } from "../Client" -import { errorLogChannel, color } from "../../../config/options.json" +import { color } from "../../../config/options.json" import { Button } from "../../interfaces" -import { Events, TextChannel } from "discord.js" +import { Events } from "discord.js" import path = require("path") import fs = require("fs") +import logToChannel from "../functions/logtochannel" type FileType = "js" | "ts" const embedColor = Number(color.replace("#", "0x")) @@ -40,14 +41,8 @@ export default function loadButtonEvents(client: Client, ft: FileType) { await button.execute(interaction) } catch (error) { if (process.env.NODE_ENV !== "dev") { - const channel = client.channels.cache.get( - errorLogChannel, - ) as TextChannel - if (!channel) { - console.log("No error log channel found.") - } - await channel.send({ + await logToChannel("error", { embeds: [ { title: "Button error occured", diff --git a/src/utils/eventHandlers/command.ts b/src/utils/eventHandlers/command.ts index 887275a..2532e00 100644 --- a/src/utils/eventHandlers/command.ts +++ b/src/utils/eventHandlers/command.ts @@ -1,9 +1,10 @@ import { ExtendedClient as Client } from "../Client" -import { errorLogChannel, color } from "../../../config/options.json" +import { color } from "../../../config/options.json" import { Command } from "../../interfaces" -import { Events, TextChannel } from "discord.js" +import { Events } from "discord.js" import path = require("path") import fs = require("fs") +import logToChannel from "../functions/logtochannel" type FileType = "js" | "ts" const embedColor = Number(color.replace("#", "0x")) @@ -41,14 +42,7 @@ export default function loadSlashCommandsEvents(client: Client, ft: FileType) { await command.execute(interaction, client) } catch (error) { if (process.env.NODE_ENV !== "dev") { - const channel = client.channels.cache.get( - errorLogChannel, - ) as TextChannel - if (!channel) { - console.log("No error log channel found.") - } - - await channel.send({ + await logToChannel("error", { embeds: [ { title: "Command error occured", diff --git a/src/utils/eventHandlers/contextmenu.ts b/src/utils/eventHandlers/contextmenu.ts index b9e2354..403e835 100644 --- a/src/utils/eventHandlers/contextmenu.ts +++ b/src/utils/eventHandlers/contextmenu.ts @@ -1,9 +1,10 @@ import { ExtendedClient as Client } from "../Client" import { ContextMenu } from "../../interfaces" -import { errorLogChannel, color } from "../../../config/options.json" -import { Events, TextChannel } from "discord.js" +import { color } from "../../../config/options.json" +import { Events } from "discord.js" import path = require("path") import fs = require("fs") +import logToChannel from "../functions/logtochannel" type FileType = "js" | "ts" const embedColor = Number(color.replace("#", "0x")) @@ -48,14 +49,7 @@ export default function loadContextMenuEvents(client: Client, ft: FileType) { await command.execute(interaction) } catch (error) { if (process.env.NODE_ENV !== "dev") { - const channel = client.channels.cache.get( - errorLogChannel, - ) as TextChannel - if (!channel) { - console.log("No error log channel found.") - } - - await channel.send({ + await logToChannel("error", { embeds: [ { title: "Contextmenu error occured", diff --git a/src/utils/eventHandlers/modal.ts b/src/utils/eventHandlers/modal.ts index f41d902..8241677 100644 --- a/src/utils/eventHandlers/modal.ts +++ b/src/utils/eventHandlers/modal.ts @@ -1,9 +1,10 @@ import { ExtendedClient as Client } from "../Client" -import { errorLogChannel, color } from "../../../config/options.json" +import { color } from "../../../config/options.json" import { Modal } from "../../interfaces" -import { Events, TextChannel } from "discord.js" +import { Events} from "discord.js" import path = require("path") import fs = require("fs") +import logToChannel from "../functions/logtochannel" type FileType = "js" | "ts" const embedColor = Number(color.replace("#", "0x")) @@ -42,14 +43,7 @@ export default function loadModalEvents(client: Client, ft: FileType) { await modal.execute(interaction) } catch (error) { if (process.env.NODE_ENV !== "dev") { - const channel = client.channels.cache.get( - errorLogChannel, - ) as TextChannel - if (!channel) { - console.log("No error log channel found.") - } - - await channel.send({ + await logToChannel("error", { embeds: [ { title: "Button error occured", diff --git a/src/utils/functions/logtochannel.ts b/src/utils/functions/logtochannel.ts new file mode 100644 index 0000000..a5cc857 --- /dev/null +++ b/src/utils/functions/logtochannel.ts @@ -0,0 +1,24 @@ +import { guildid, onlineLogChannel, botLogChannel, guildLogChannel, errorLogChannel } from "../../../config/options.json" +import { Guild, MessageCreateOptions, TextChannel } from "discord.js" +import Illegitimate from "../Illegitimate" + +const channels = { + online: onlineLogChannel, + bot: botLogChannel, + guild: guildLogChannel, + error: errorLogChannel +} + +type Channel = keyof typeof channels + +export default async function logToChannel(channel: Channel, message: MessageCreateOptions): Promise { + const guild = Illegitimate.client.guilds.cache.get(guildid) as Guild + + const logChannel = guild.channels.cache.get(channels[channel]) as TextChannel + if (!logChannel) { + console.log(`[ERROR] Could not find channel used for ${channel} logging.`) + return + } + + await logChannel.send(message) +} \ No newline at end of file