Skip to content

Commit 5c06522

Browse files
committed
Automatically remove bot's messages when commands are deleted
1 parent 01b7bdd commit 5c06522

File tree

4 files changed

+65
-19
lines changed

4 files changed

+65
-19
lines changed

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"dotenv": "8.2.0",
2929
"erlpack": "github:discordapp/erlpack",
3030
"isolated-vm": "3.3.3",
31+
"node-cache": "5.1.2",
3132
"node-fetch": "2.6.0",
3233
"polish-plurals": "1.1.0",
3334
"request": "2.88.2",

src/app.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { handleCommand } from './commands';
66
import { getConfig } from './config';
77
import { InvalidUsageError } from './types';
88

9+
import Cache from 'node-cache';
10+
const ONE_HOUR_S = 3600;
11+
const cache = new Cache({ stdTTL: ONE_HOUR_S });
12+
913
const client = new Discord.Client();
1014
const drss = new DiscordRSS.Client({
1115
database: {
@@ -48,6 +52,10 @@ client.on('debug', (debug) => {
4852
}
4953
});
5054

55+
function isCommand(msg: Discord.Message) {
56+
return msg.content.startsWith(getConfig('PREFIX'));
57+
}
58+
5159
client.on('message', async (msg) => {
5260
if (msg.author.bot) {
5361
return;
@@ -57,9 +65,15 @@ client.on('message', async (msg) => {
5765
return msg.channel.send(`┬─┬ノ( ◕◡◕ ノ)`);
5866
}
5967

60-
if (msg.content.startsWith(getConfig('PREFIX'))) {
68+
if (isCommand(msg)) {
6169
try {
70+
const collector = msg.channel.createMessageCollector(
71+
(m: Discord.Message) => m.author.id === client.user.id
72+
);
6273
await handleCommand(msg);
74+
const ids = collector.collected.map((m) => m.id);
75+
cache.set(msg.id, ids);
76+
collector.stop();
6377
} catch (err) {
6478
if (err instanceof InvalidUsageError) {
6579
void msg.reply(err.message);
@@ -75,6 +89,30 @@ client.on('message', async (msg) => {
7589
return;
7690
});
7791

92+
async function revertCommand(msg: Discord.Message) {
93+
if (!cache.has(msg.id)) {
94+
return undefined;
95+
}
96+
const messagesToDelete = cache.get<string[]>(msg.id)!;
97+
return msg.channel.bulkDelete(messagesToDelete);
98+
}
99+
100+
client.on('messageDelete', async (msg) => {
101+
if (msg.author.bot) {
102+
return;
103+
}
104+
105+
if (isCommand(msg)) {
106+
try {
107+
await revertCommand(msg);
108+
} catch (err) {
109+
errors.push(err);
110+
}
111+
}
112+
113+
return;
114+
});
115+
78116
async function init() {
79117
await client.login(getConfig('DISCORD_BOT_TOKEN'));
80118
drss._defineBot(client);

src/commands/index.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import wiki from './wiki';
2424

2525
const COMMAND_PATTERN = new RegExp(getConfig('PREFIX') + '([a-z]+)(?: (.*))?');
2626

27-
const allCommands = {
27+
const allCommands = [
2828
co,
2929
execute,
3030
link,
@@ -44,7 +44,7 @@ const allCommands = {
4444
youtube,
4545
typeofweb,
4646
wiki,
47-
};
47+
];
4848

4949
const cooldowns = new Discord.Collection<string, Discord.Collection<string, number>>();
5050
const PERMISSION_TO_OVERRIDE_COOLDOWN: PermissionString = 'ADMINISTRATOR';
@@ -86,16 +86,11 @@ async function verifyCooldown(msg: Discord.Message, command: Command) {
8686
}
8787

8888
function printHelp(msg: Discord.Message, member: Discord.GuildMember) {
89-
const commands = Object.entries(allCommands)
90-
.sort(([a], [b]) => {
91-
if (a > b) {
92-
return 1;
93-
} else if (a < b) {
94-
return -1;
95-
}
96-
return 1;
89+
const commands = allCommands
90+
.sort((a, b) => {
91+
return a.name.localeCompare(b.name);
9792
})
98-
.filter(([, command]) => {
93+
.filter((command) => {
9994
if (command.permissions && !member.hasPermission(command.permissions)) {
10095
return false;
10196
}
@@ -104,8 +99,8 @@ function printHelp(msg: Discord.Message, member: Discord.GuildMember) {
10499

105100
const data = [
106101
`**Oto lista wszystkich komend:**`,
107-
...commands.map(([name, command]) => {
108-
return `**\`${getConfig('PREFIX')}${name}\`** — ${command.description}`;
102+
...commands.map((command) => {
103+
return `**\`${getConfig('PREFIX')}${command.name}\`** — ${command.description}`;
109104
}),
110105
];
111106

@@ -136,20 +131,19 @@ export async function handleCommand(msg: Discord.Message) {
136131
return printHelp(msg, member);
137132
}
138133

139-
if (!maybeCommand || !(maybeCommand in allCommands)) {
134+
const command = allCommands.find((c) => maybeCommand === c.name);
135+
136+
if (!command || !maybeCommand) {
140137
return undefined;
141138
}
142139

143-
const commandName = maybeCommand as keyof typeof allCommands;
144-
145-
const command = allCommands[commandName];
146140
const member = await msg.guild.fetchMember(msg.author);
147141

148142
if (command.permissions && !member.hasPermission(command.permissions)) {
149143
return undefined; // silence is golden
150144
}
151145

152-
void msg.channel.startTyping();
146+
msg.channel.startTyping();
153147

154148
if (command.guildOnly && msg.channel.type !== 'text') {
155149
throw new InvalidUsageError(`to polecenie można wywołać tylko na kanałach.`);

0 commit comments

Comments
 (0)