Skip to content

Commit

Permalink
fix(Discord): add error messages if channel is not found or client no…
Browse files Browse the repository at this point in the history
…t initialized
  • Loading branch information
sogehige committed Mar 28, 2024
1 parent e284085 commit 540e493
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/plugins/Discord.ts
@@ -1,11 +1,11 @@
import { ChannelType, TextChannel } from 'discord.js';

import { getUserSender } from '~/helpers/commons/index.js';
import { chatOut } from '~/helpers/log.js';
import { chatOut, error } from '~/helpers/log.js';
import getBotId from '~/helpers/user/getBotId.js';
import getBotUserName from '~/helpers/user/getBotUserName.js';

export const DiscordGenerator = (pluginId: string, userstate: { userName: string, userId: string } | null) => ({
export const DiscordGenerator = (pluginId: string, fileName: string) => ({
sendMessage: async (channelName: string, message: string) => {
const Discord = (await import('../integrations/discord.js') as typeof import('../integrations/discord')).default;
const Message = (await import('../message.js') as typeof import('../message')).Message;
Expand All @@ -19,18 +19,25 @@ export const DiscordGenerator = (pluginId: string, userstate: { userName: string

// search discord channel by ID
if (Discord.client) {
let channelFound = false;
for (const [ id, channel ] of Discord.client.channels.cache) {
if (channel.type === ChannelType.GuildText) {
if (id === channelName || (channel as TextChannel).name === channelName) {
const ch = Discord.client.channels.cache.find(o => o.id === id);
if (ch) {
(ch as TextChannel).send(await Discord.replaceLinkedUsernameInMessage(message));
chatOut(`#${(ch as TextChannel).name}: ${message} [${Discord.client.user?.tag}]`);
channelFound = true;
break;
}
}
}
}
if (!channelFound) {
error(`PLUGINS#${pluginId}:./${fileName}: Channel not found: ${channelName}`);
}
} else {
error(`PLUGINS#${pluginId}:./${fileName}: Discord client is not initialized`);
}

},
});
2 changes: 1 addition & 1 deletion src/plugins/Sandbox.ts
Expand Up @@ -69,7 +69,7 @@ export const runScriptInSandbox = (plugin: Plugin,
// @ts-expect-error TS6133
const ListenTo = ListenToGenerator(plugin.id, type, message, userstate, params);
// @ts-expect-error TS6133
const Discord = DiscordGenerator(plugin.id, userstate);
const Discord = DiscordGenerator(plugin.id, ___code___.name);
// @ts-expect-error TS6133
const Twitch = TwitchGenerator(plugin.id, userstate);
// @ts-expect-error TS6133
Expand Down

0 comments on commit 540e493

Please sign in to comment.