-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathetc.ts
154 lines (144 loc) · 4.33 KB
/
etc.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import {
ThreadAutoArchiveDuration,
Message,
MessageReaction,
User,
ThreadChannel,
} from 'discord.js';
import { Bot } from '../bot';
import { suggestionsChannelId } from '../env';
import {
clearMessageOwnership,
DELETE_EMOJI,
ownsBotMessage,
} from '../util/send';
const emojiRegex = /<:\w+?:(\d+?)>|(\p{Emoji_Presentation})/gu;
const defaultPollEmojis = ['✅', '❌', '🤷'];
export function etcModule(bot: Bot) {
bot.registerCommand({
aliases: ['ping'],
description: 'See if the bot is alive',
async listener(msg) {
await msg.channel.send('pong. :ping_pong:');
},
});
bot.client.on('messageCreate', async msg => {
if (msg.author.bot || !msg.content.toLowerCase().startsWith('poll:'))
return;
let emojis = [
...new Set(
[...msg.content.matchAll(emojiRegex)].map(x => x[1] ?? x[2]),
),
];
if (!emojis.length) emojis = defaultPollEmojis;
for (const emoji of emojis) await msg.react(emoji);
});
bot.client.on('messageCreate', async msg => {
if (msg.author.bot || msg.channelId !== suggestionsChannelId) return;
// First 50 characters of the first line of the content (without cutting off a word)
const title =
msg.content
.split('\n')[0]
.split(/(^.{0,50}\b)/)
.find(x => x) ?? 'Suggestion';
await msg.startThread({
name: title,
autoArchiveDuration: ThreadAutoArchiveDuration.OneDay,
});
for (let emoji of defaultPollEmojis) await msg.react(emoji);
});
bot.client.on('threadUpdate', async thread => {
if (
thread.parentId !== suggestionsChannelId ||
!((await thread.fetch()) as ThreadChannel).archived
)
return;
const channel = thread.parent!;
let lastMessage = null;
let suggestion: Message;
while (!suggestion!) {
const msgs = await channel.messages.fetch({
before: lastMessage ?? undefined,
limit: 5,
});
suggestion = msgs.find(msg => msg.thread?.id === thread.id)!;
lastMessage = msgs.last()!.id as string;
}
const pollingResults = defaultPollEmojis.map(emoji => {
const reactions = suggestion.reactions.resolve(emoji);
// Subtract the bot's vote
const count = (reactions?.count ?? 0) - 1;
return [emoji, count] as const;
});
const pollingResultStr = pollingResults
.sort((a, b) => b[1] - a[1])
.map(([emoji, count]) => `${count} ${emoji}`)
.join(' ');
await suggestion.reply({
content: `Polling finished; result: ${pollingResultStr}`,
});
});
bot.client.on('messageReactionAdd', async (reaction, member) => {
if (reaction.partial) return;
if ((await reaction.message.fetch()).author.id !== bot.client.user.id)
return;
if (reaction.emoji.name !== DELETE_EMOJI) return;
if (member.id === bot.client.user.id) return;
if (ownsBotMessage(reaction.message, member.id)) {
clearMessageOwnership(reaction.message);
await reaction.message.delete();
} else {
await reaction.users.remove(member.id);
}
});
bot.registerAdminCommand({
aliases: ['kill'],
async listener(msg) {
const confirm = '✅';
const confirmationMessage = await msg.channel.send('Confirm?');
confirmationMessage.react(confirm);
const reactionFilter = (reaction: MessageReaction, user: User) =>
reaction.emoji.name === confirm && user.id === msg.author.id;
const proceed = await confirmationMessage
.awaitReactions({
filter: reactionFilter,
max: 1,
time: 10 * 1000,
errors: ['time'],
})
.then(() => true)
.catch(() => false);
await confirmationMessage.delete();
if (!proceed) return;
await msg.react('☠️');
process.stdout.write(`
,--.
{ }
K, }
/ ~Y\`
, / /
{_'-K.__/
\`/-.__L._
/ ' /\`\\_}
/ ' /
____ / ' /
,-'~~~~ ~~/ ' /_
,' \`\`~~~ ',
( Y
{ I
{ - \`,
| ', )
| | ,..__ __. Y
| .,_./ Y ' / ^Y J )|
\ |' / | | || Killed by @${msg.author.tag}/${msg.author.id}
\ L_/ . _ (_,.'(
\, , ^^""' / | )
\_ \ /,L] /
'-_~-, \` \` ./\`
\`'{_ )
^^\..___,.--\`
`);
process.exit(1);
},
});
}