-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
122 lines (100 loc) · 4.37 KB
/
index.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
import DiscondJS, { Channel, Intents } from 'discord.js'
import dotenv from 'dotenv'
dotenv.config()
const client = new DiscondJS.Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_PRESENCES
]
})
const maxNumVotes = 6
const approvedEmoji = '✅'
// -----------------------------------------------------------------------------------------
client.on('ready', () => {
console.log('the bot is ready!')
const guild = client.guilds.cache.get(process.env.GUILD_ID ?? "")
let commands = guild ? guild.commands : client.application?.commands
commands?.create({
name: 'ping',
description: '🏓 missing reviewers'
})
commands?.create({
name: 'autocomplete',
description: '🏓 missing reviewers'
})
})
// -----------------------------------------------------------------------------------------
client.on('messageReactionAdd', async (reaction) => {
if (reaction.emoji.name === approvedEmoji &&
reaction.message.author &&
Number(reaction.count) >= maxNumVotes) {
const authorID = reaction.message.author?.id
const approvedMessage = 'Approved! 🎉✅'
const message = '<@' + authorID + '>' + ' ' + approvedMessage
const threadMessages = await reaction.message.thread?.messages.fetch()
if (threadMessages)
{
let botMessage = threadMessages.find(message => message.author.bot == true && message.content.includes(approvedMessage) )
if (botMessage) return
}
sendApprovedMessage(message, authorID, reaction)
}
})
function sendApprovedMessage(messageContent: string, authorID: string, reactionObject: any) {
if (reactionObject.message.hasThread) {
reactionObject.message.thread?.send({
content: messageContent,
allowedMentions: { users: [authorID] }
})
}
else {
reactionObject.message.reply({
content: messageContent,
allowedMentions: { users: [authorID] }
})
}
}
// -----------------------------------------------------------------------------------------
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return
if (interaction.commandName === 'ping') {
if (interaction.channel?.isThread()) {
const msg = await interaction.channel.fetchStarterMessage()
var members: string[] = new Array()
msg.mentions.roles.forEach(element => {
var userIds = element.members.filter(member => (member.user.bot == false) && (msg.author.id != member.user.id)).map(mapMember => mapMember.user.id)
members = members.concat(userIds)
})
const mentionMembers = new Set(members)
const reaction = msg.reactions.cache.get(approvedEmoji)
if (mentionMembers.size > 0) {
if (reaction) {
const reactionUserArray = await reaction.users.fetch()
const reactionUser = new Set(reactionUserArray.map(mapMember => mapMember.id))
const membersMissingReview = [...mentionMembers].filter(user => !reactionUser.has(user))
if (membersMissingReview.length > 0) {
const pingMembers = membersMissingReview.join('><@')
await interaction.reply({ content: '<@' + pingMembers + '>' + ' 🏓' })
}
else {
await interaction.reply({ content: 'Everyone has reviewed the code, yupiii!!! 🎉', ephemeral: true })
}
}
else {
const pingMembers = [...mentionMembers].join('><@')
await interaction.reply({ content: '<@' + pingMembers + '>' + ' 🏓' })
}
} else {
await interaction.reply({ content: 'The review message doesnt have reviewers', ephemeral: true })
}
}
else {
await interaction.reply({ content: 'Unable to ping the reviewers, the chat is not a thread!, 💀', ephemeral: true })
}
}
})
// -----------------------------------------------------------------------------------------
client.login(process.env.TOKEN)