Skip to content

Commit

Permalink
feat(plugins): add Discord.sendMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Mar 23, 2024
1 parent 66aa04a commit 32c82b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/plugins/Discord.ts
@@ -0,0 +1,36 @@
import { ChannelType, TextChannel } from 'discord.js';

import { getUserSender } from '~/helpers/commons/index.js';
import { chatOut } 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) => ({
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;

// remove # from channel name
if (channelName.startsWith('#')) {
channelName = channelName.slice(1);
}

message = await new Message(message).parse({ sender: getUserSender(getBotId(), getBotUserName()), replaceCustomVariables: true, discord: undefined }) as string;

// search discord channel by ID
if (Discord.client) {
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}]`);
}
}
}
}
}

},
});
3 changes: 3 additions & 0 deletions src/plugins/Sandbox.ts
Expand Up @@ -4,6 +4,7 @@ import axios, { AxiosRequestConfig } from 'axios';
import ts from 'typescript';

import { CustomVariableGenerator } from './CustomVariable.js';
import { DiscordGenerator } from './Discord.js';
import { ListenToGenerator, Types } from './ListenTo.js';
import { LogGenerator } from './Log.js';
import { PermissionGenerator } from './Permission.js';
Expand Down Expand Up @@ -68,6 +69,8 @@ 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);
// @ts-expect-error TS6133
const Twitch = TwitchGenerator(plugin.id, userstate);
// @ts-expect-error TS6133
const Permission = PermissionGenerator(plugin.id);
Expand Down

0 comments on commit 32c82b4

Please sign in to comment.