-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
170 lines (154 loc) · 6.88 KB
/
bot.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const Discord = require("discord.js")
const bot = new Discord.Client({
allowedMentions: {
repliedUser: false,
parse: ["users"]
},
failIfNotExists: false,
intents: [
Discord.GatewayIntentBits.Guilds
]
})
const { botToken } = require("./config.json")
bot.login(botToken)
module.exports = bot
const oauth = require("./util/oauth.js")
const pool = require("./util/setupDB.js")
const registerSlashcommands = () => {
bot.application.commands.set([{
name: "hook",
type: Discord.ApplicationCommandType.ChatInput,
description: "Create and edit GitHub event webhooks",
defaultMemberPermissions: ["ManageGuild"],
dmPermission: false,
options: [{
name: "create",
type: Discord.ApplicationCommandOptionType.Subcommand,
description: "Create a new webhook",
options: [{
name: "name",
type: Discord.ApplicationCommandOptionType.String,
description: "The name of the webhook",
maxLength: 32,
required: true
},{
name: "events",
type: Discord.ApplicationCommandOptionType.String,
description: "Events to trigger the webhook, separated by commas",
maxLength: 220
},{
name: "actions",
type: Discord.ApplicationCommandOptionType.String,
description: "Actions (of events) to trigger the webhook, separated by commas",
maxLength: 220
}]
},{
name: "edit-message",
type: Discord.ApplicationCommandOptionType.Subcommand,
description: "Edit the message sent on an event",
options: [{
name: "hook",
type: Discord.ApplicationCommandOptionType.String,
description: "The webhook to edit",
maxLength: 32,
autocomplete: true,
required: true
},{
name: "message",
type: Discord.ApplicationCommandOptionType.String,
description: "The new message as template name, JSON object or array",
required: true
}]
},{
name: "delete",
type: Discord.ApplicationCommandOptionType.Subcommand,
description: "Delete a webhook",
options: [{
name: "hook",
type: Discord.ApplicationCommandOptionType.String,
description: "The webhook to delete",
maxLength: 32,
autocomplete: true,
required: true
}]
}]
}])
}
bot.on("ready", () => {
bot.user.setPresence({activities: [{name: "Custom Status", state: "Customizable GitHub hooks!", type: Discord.ActivityType.Custom}], status: "online"})
if (Math.random() > 0.95) registerSlashcommands()
})
bot.on("guildCreate", guild => {
const embed = new Discord.MessageEmbed()
.setColor(Discord.Colors.Green)
.setTitle(guild.name)
.setThumbnail(guild.iconURL())
.addField("Member count", "" + guild.memberCount, true)
.addField("Owner", "<@" + guild.ownerId + ">", true)
bot.channels.cache.get("1169875077110693951").send({embeds: [embed]})
})
bot.on("guildDelete", guild => {
pool.query("DELETE FROM `hook` WHERE `guild` = ?", [guild.id])
const embed = new Discord.MessageEmbed()
.setColor(Discord.Colors.Red)
.setTitle(guild.name)
.setThumbnail(guild.iconURL())
.addField("Member count", "" + guild.memberCount, true)
.addField("Owner", "<@" + guild.ownerId + ">", true)
bot.channels.cache.get("1169875077110693951").send({embeds: [embed]})
})
const channelOrWebhookRow = new Discord.ActionRowBuilder()
.addComponents(
new Discord.ChannelSelectMenuBuilder()
.setCustomId("setup_channel")
.setChannelTypes([Discord.ChannelType.GuildText, Discord.ChannelType.GuildAnnouncement, Discord.ChannelType.GuildVoice, Discord.ChannelType.GuildStageVoice, Discord.ChannelType.GuildForum])
)
bot.on("interactionCreate", async interaction => {
if (interaction.type == Discord.InteractionType.ApplicationCommand) {
if (interaction.commandName == "hook") {
const subcommand = interaction.options.getSubcommand()
if (subcommand == "create") {
const msg = await interaction.reply({
content: "You're creating the webhook **" + Discord.escapeMarkdown(interaction.options.getString("name")) + "**.\n\nNow, choose the channel to send the messages to.\n**Tip**: Start typing and the channels matching your input will appear.",
components: [channelOrWebhookRow],
fetchReply: true,
ephemeral: true
})
msg.createMessageComponentCollector({componentType: Discord.ComponentType.ChannelSelect}).on("collect", async i => {
let id = oauth.generateToken(8)
while (true) {
const [rows] = await pool.query("SELECT * FROM `hook` WHERE `id` = ?", [id])
if (rows.length == 0) break
id = oauth.generateToken(8)
}
const secret = oauth.generateToken()
await pool.query(
"INSERT INTO `hook` (`id`, `name`, `server`, `webhook`, `channel`, `message`, `secret`, `filterEvent`, `filterAction`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
[id, interaction.options.getString("name"), interaction.guild.id, null, i.values[0], null, secret,
interaction.options.getString("events") ? JSON.stringify(interaction.options.getString("events").split(",")) : null,
interaction.options.getString("actions") ? JSON.stringify(interaction.options.getString("actions").split(",")) : null]
)
i.reply({
content: "Successfully created the hook!\n\nYou can now use it on GitHub to receive events in <#" + i.values[0] + ">.\n\n" +
"Full POST URL: `https://disgithook-api.tomatenkuchen.com/hook/" + id + "/" + secret +
"`\nMore secure POST URL with secret in header: `https://disgithook-api.tomatenkuchen.com/hook/" + id + "`\nSecret: `" + secret + "`",
ephemeral: true
})
})
} else if (subcommand == "edit-message") {
const [rows] = await pool.query("SELECT * FROM `hook` WHERE `id` = ? AND `server` = ?", [interaction.options.getString("hook"), interaction.guild.id])
if (rows.length == 0) return interaction.reply({content: "The hook `" + interaction.options.getString("hook") + "` doesn't exist or doesn't belong to this server.", ephemeral: true})
await pool.query("UPDATE `hook` SET `message` = ? WHERE `id` = ?", [interaction.options.getString("message"), interaction.options.getString("hook")])
interaction.reply({content: "Successfully updated the hook message of **" + rows[0].name + "**.", ephemeral: true})
} else if (subcommand == "delete") {
const [rows] = await pool.query("SELECT * FROM `hook` WHERE `id` = ? AND `server` = ?", [interaction.options.getString("hook"), interaction.guild.id])
if (rows.length == 0) return interaction.reply({content: "The hook `" + interaction.options.getString("hook") + "` doesn't exist or doesn't belong to this server.", ephemeral: true})
await pool.query("DELETE FROM `hook` WHERE `id` = ?", [interaction.options.getString("hook")])
interaction.reply({content: "Successfully deleted the hook **" + rows[0].name + "**.", ephemeral: true})
}
}
} else if (interaction.type == Discord.InteractionType.ApplicationCommandAutocomplete) {
const [rows] = await pool.query("SELECT * FROM `hook` WHERE `server` = ?", [interaction.guild.id])
interaction.respond(rows.slice(0, 25).map(hook => ({name: hook.name, value: hook.id})))
}
})