-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.ts
More file actions
65 lines (56 loc) · 1.96 KB
/
module.ts
File metadata and controls
65 lines (56 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import * as config from '../config';
const sticky = new pylon.KVNamespace('stickymessages');
discord.interactions.commands.register(
{
name: 'sticky',
description: 'Sticky a message in this channel',
ackBehavior: discord.interactions.commands.AckBehavior.MANUAL,
options: (opts) => ({
message: opts.string({
required: true,
description: 'The message to sticky',
}),
}),
},
async (interaction, { message }) => {
if (!interaction.member.can(discord.Permissions.MANAGE_MESSAGES))
return interaction.respondEphemeral(
'You do not have permission to sticky messages.'
);
await sticky.put(interaction.channelId, message);
var channel = await discord.getTextChannel(interaction.channelId);
if (!channel) throw new Error('[STICKY] Something went wrong.');
channel.sendMessage(message).then(async (msg) => {
await sticky.put(`${interaction.channelId}.recentMessage`, msg.id);
});
interaction.respondEphemeral('Message successfully stickied!');
}
);
discord.on(discord.Event.MESSAGE_CREATE, async (message) => {
var items = await sticky.list();
if (items.includes(message.channelId)) {
var stickied = await sticky.get<string>(message.channelId);
var recent = await sticky.get<string>(`${message.channelId}.recentMessage`);
if (!recent) return;
var channel = await message.getChannel();
var msg = await channel.getMessage(recent).catch((err) => {});
if (!msg) return;
message.reply(<string>stickied).then(async (msg) => {
await sticky.put(`${message.channelId}.recentMessage`, msg.id);
});
msg.delete().catch((err) => {
return;
});
}
});
discord.on(discord.Event.MESSAGE_DELETE, async (message) => {
var recent = await sticky
.get<string>(`${message.channelId}.recentMessage`)
.catch((err) => {
return;
});
if (recent == message.id) {
await sticky.delete(message.channelId);
console.log('removed sticky');
}
});