1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-17 20:30:58 -05:00

Compare commits

..

3 Commits

Author SHA1 Message Date
toast-ts
b8c760121b Finetune the raw gateway events 2024-01-23 08:57:07 +11:00
toast-ts
79a3ff8fd9 Add raw gateway packets for messageUpdate and messageDelete 2024-01-23 07:13:59 +11:00
Toast
0ccf1761cc
Update ResponseModule.ts 2024-01-23 05:58:06 +11:00
5 changed files with 66 additions and 7 deletions

View File

@ -1,10 +1,10 @@
import Discord from 'discord.js'; import Discord from 'discord.js';
import TClient from '../client.js'; import TClient from '../client.js';
import Logger from '../helpers/Logger.js'; import Logger from '../helpers/Logger.js';
import {disabledChannels} from '../index.js';
export default class MessageDelete { export default class MessageDelete {
static run(client:TClient, msg:Discord.Message){ static run(client:TClient, msg:Discord.Message){
if (!client.config.botSwitches.logs) return; if (!client.config.botSwitches.logs) return;
const disabledChannels = ['548032776830582794', '541677709487505408', '949380187668242483']
if (msg.guild?.id != client.config.dcServer.id || msg.partial || msg.author.bot || disabledChannels.includes(msg.channelId)) return; if (msg.guild?.id != client.config.dcServer.id || msg.partial || msg.author.bot || disabledChannels.includes(msg.channelId)) return;
if (Discord.DiscordAPIError.name === '10008') return Logger.console('log', 'MsgDelete', 'Caught an unexpected error returned by Discord API. (Unknown Message)'); if (Discord.DiscordAPIError.name === '10008') return Logger.console('log', 'MsgDelete', 'Caught an unexpected error returned by Discord API. (Unknown Message)');
const embed = new client.embed().setColor(client.config.embedColorRed).setTimestamp().setAuthor({name: `Author: ${msg.author.username} (${msg.author.id})`, iconURL: `${msg.author.displayAvatarURL()}`}).setTitle('Message deleted').setDescription(`<@${msg.author.id}>\n\`${msg.author.id}\``); const embed = new client.embed().setColor(client.config.embedColorRed).setTimestamp().setAuthor({name: `Author: ${msg.author.username} (${msg.author.id})`, iconURL: `${msg.author.displayAvatarURL()}`}).setTitle('Message deleted').setDescription(`<@${msg.author.id}>\n\`${msg.author.id}\``);

View File

@ -1,12 +1,15 @@
import Discord from 'discord.js'; import Discord from 'discord.js';
import TClient from '../client.js'; import TClient from '../client.js';
import MessageTool from '../helpers/MessageTool.js'; import MessageTool from '../helpers/MessageTool.js';
import {disabledChannels, rawSwitches} from '../index.js';
export default class MessageUpdate { export default class MessageUpdate {
static async run(client:TClient, oldMsg:Discord.Message, newMsg:Discord.Message){ static async run(client:TClient, oldMsg:Discord.Message, newMsg:Discord.Message){
if (!client.config.botSwitches.logs) return; if (!client.config.botSwitches.logs) return;
if (oldMsg.guild?.id != client.config.dcServer.id || oldMsg.author === null || oldMsg?.author.bot || oldMsg.partial || newMsg.partial || !newMsg.member || ['548032776830582794', '541677709487505408', '949380187668242483'].includes(newMsg.channelId)) return; if (oldMsg.guild?.id != client.config.dcServer.id || oldMsg.author === null || oldMsg?.author.bot || oldMsg.partial || newMsg.partial || !newMsg.member || disabledChannels.includes(newMsg.channelId)) return;
if (await client.prohibitedWords.findWord(newMsg.content.toLowerCase().replaceAll(/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?\n?0-9]|[]|ing\b/g, '').split(' ').join('')) && (!MessageTool.isStaff(newMsg.member))) newMsg.delete(); if (await client.prohibitedWords.findWord(newMsg.content.toLowerCase().replaceAll(/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?\n?0-9]|[]|ing\b/g, '').split(' ').join('')) && (!MessageTool.isStaff(newMsg.member))) newMsg.delete();
if (newMsg.content === oldMsg.content) return; if (!rawSwitches.MESSAGE_UPDATE || (rawSwitches.MESSAGE_UPDATE && newMsg.content !== oldMsg.content)) {
(client.channels.resolve(client.config.dcServer.channels.logs) as Discord.TextChannel).send({embeds: [new client.embed().setColor(client.config.embedColor).setTimestamp().setAuthor({name: `Author: ${oldMsg.author.username} (${oldMsg.author.id})`, iconURL: oldMsg.author.displayAvatarURL()}).setTitle('Message edited').setDescription(`<@${oldMsg.author.id}>\n\`${oldMsg.author.id}\``).addFields({name: 'Old content', value: `\`\`\`${oldMsg.content.length < 1 ? '(Attachment)' : Discord.escapeCodeBlock(oldMsg.content.slice(0,2048))}\`\`\``}, {name: 'New content', value: `\`\`\`${Discord.escapeCodeBlock(newMsg.content.slice(0,2048))}\`\`\``}, {name: 'Channel', value: `<#${oldMsg.channelId}>`})], components: [new Discord.ActionRowBuilder<Discord.ButtonBuilder>().addComponents(new Discord.ButtonBuilder().setStyle(5).setURL(oldMsg.url).setLabel('Jump to message'))]}); rawSwitches.MESSAGE_UPDATE = true;
(client.channels.resolve(client.config.dcServer.channels.logs) as Discord.TextChannel).send({embeds: [new client.embed().setColor(client.config.embedColor).setTimestamp().setAuthor({name: `Author: ${oldMsg.author.username} (${oldMsg.author.id})`, iconURL: oldMsg.author.displayAvatarURL()}).setTitle('Message edited').setDescription(`<@${oldMsg.author.id}>\n\`${oldMsg.author.id}\``).addFields({name: 'Old content', value: `\`\`\`${oldMsg.content.length < 1 ? '(Attachment)' : Discord.escapeCodeBlock(oldMsg.content.slice(0,2048))}\`\`\``}, {name: 'New content', value: `\`\`\`${Discord.escapeCodeBlock(newMsg.content.slice(0,2048))}\`\`\``}, {name: 'Channel', value: `<#${oldMsg.channelId}>`})], components: [new Discord.ActionRowBuilder<Discord.ButtonBuilder>().addComponents(new Discord.ButtonBuilder().setStyle(5).setURL(oldMsg.url).setLabel('Jump to message'))]});
}
} }
} }

View File

@ -6,8 +6,9 @@ import Logger from './helpers/Logger.js';
import YTModule from './modules/YTModule.js'; import YTModule from './modules/YTModule.js';
import MPModule, {refreshTimerSecs} from './modules/MPModule.js'; import MPModule, {refreshTimerSecs} from './modules/MPModule.js';
import UsernameHelper from './helpers/UsernameHelper.js'; import UsernameHelper from './helpers/UsernameHelper.js';
import {Punishment} from 'src/interfaces'; import {Punishment, RawGatewayPacket, RawMessageDelete, RawMessageUpdate} from 'src/interfaces';
import {readFileSync} from 'node:fs'; import {readFileSync} from 'node:fs';
export const disabledChannels = ['548032776830582794', '541677709487505408', '949380187668242483'];
// Error handler // Error handler
function _(error:Error, type:string) { function _(error:Error, type:string) {
@ -69,3 +70,36 @@ if (client.config.botSwitches.dailyMsgsBackup) {
client.userLevels.dataSweeper(); client.userLevels.dataSweeper();
} }
// Cronjob tasks // Cronjob tasks
// Raw gateway event receivers
export let rawSwitches = {
MESSAGE_UPDATE: false,
MESSAGE_DELETE: false
};
client.on('raw', async (packet:RawGatewayPacket<RawMessageUpdate>)=>{
if (rawSwitches[packet.t]) return;
if (packet.t !== 'MESSAGE_UPDATE') return;
if (packet.d.guild_id != client.config.dcServer.id || disabledChannels.includes(packet.d.channel_id)) return;
if (typeof packet.d.content === 'undefined') return;
const channel = client.channels.cache.get(packet.d.channel_id) as Discord.TextBasedChannel;
const new_message = await channel.messages.fetch(packet.d.id);
client.emit('messageUpdate', new_message, new_message);
});
client.on('raw', async (packet:RawGatewayPacket<RawMessageDelete>)=>{
if (rawSwitches[packet.t]) return;
if (packet.t !== 'MESSAGE_DELETE' || packet.d.guild_id != client.config.dcServer.id || disabledChannels.includes(packet.d.channel_id)) return;
(client.channels.resolve(client.config.dcServer.channels.logs) as Discord.TextChannel).send({
embeds: [new client.embed()
.setColor(client.config.embedColorRed)
.setTitle('Message deleted')
.setDescription('Unknown author')
.addFields(
{name: 'Received over raw API gateway', value: '\u200b'},
{name: 'Channel', value: `<#${packet.d.channel_id}>`},
).setTimestamp()
]
});
rawSwitches[packet.t] = true;
});

24
src/interfaces.d.ts vendored
View File

@ -1,4 +1,4 @@
import {ColorResolvable, PresenceData} from 'discord.js'; import {ColorResolvable, PresenceData, APIUser} from 'discord.js';
export interface Punishment { export interface Punishment {
case_id: number; case_id: number;
@ -163,3 +163,25 @@ export interface Config {
} }
} }
} }
// Credits to FlyingSixtySix/neurobot for inspiration.
// https://github.com/FlyingSixtySix/neurobot/blob/0dee4ea4f72872e2df240700eb56e1d38da1f8bb/src/interactions/jp.ts#L37-L85
export interface RawGatewayPacket<T> {
t: 'MESSAGE_DELETE'|'MESSAGE_UPDATE';
d: T;
}
export interface RawMessageDelete {
id: string,
channel_id: string,
guild_id: string
}
export interface RawMessageUpdate {
id: string,
channel_id: string,
guild_id: string,
content: string,
embeds: any[],
components: any[],
attachments: any[],
author: APIUser
member: { roles: any[] }
}

View File

@ -10,7 +10,7 @@ export default class Response {
}, },
afternoon: { afternoon: {
prefix: ['good'], prefix: ['good'],
suffix: ['all', 'everyone', 'lads'] suffix: ['all', 'everyone', 'lads', 'yall', 'y\'all']
}, },
evening: { evening: {
prefix: ['good'], prefix: ['good'],