-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord-bot.js
216 lines (174 loc) · 8.74 KB
/
discord-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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { Client, GatewayIntentBits, ActionRowBuilder, StringSelectMenuBuilder, ActivityType, PermissionsBitField } from 'discord.js';
import { faker } from '@faker-js/faker';
import { addDiscordToDb, addRoleToRoom, createClaimCode, createCommands, createDiscordRoom } from './data/create.js';
import { checkDiscordRoomCount, findUserRooms, getUserRooms } from './data/find.js';
import 'dotenv/config';
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages]
});
const TOKEN = process.env.DISCORDTOKEN
client.once('ready', () => {
console.log('Bot is ready!');
client.user.setPresence({ activities: [{ name: '/help to get started', type: ActivityType.Watching }], status: 'online' })
client.guilds.cache.forEach(guild => {
createCommands(guild)
});
client.on('guildCreate', async (guild) => {
const discordId = guild.id;
try {
await addDiscordToDb(discordId);
}
catch {
console.log('Error adding guild to database');
}
createCommands(guild);
})
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (commandName === 'help') {
if (interaction.member.permissionsIn(interaction.channel).has(PermissionsBitField.Flags.Administrator)) {
await interaction.reply({ content:
"# **[Discreetly](https://app.discreetly.chat)** \n ## ***Admins*** \n - Admins **MUST** create a room and join it first or be in a previous Discreetly discord room \n `/createroom roomname` (roomname is optional) - If you don't provide a roomname, your rooms name will be random \n - If you are already in Discreetly discord rooms and you want to add those rooms to your discord server use `/addroletoroom` \n - Users can request codes to join the rooms associated with this discord server using `/goanon` \n - `/discreetlysay` 'message' will send a message in the current Discord channel anonymously" , ephemeral: true })
} else {
await interaction.reply({
content: "# **[Discreetly](https://app.discreetly.chat)** \n ## ***Users*** \n - Users can request codes to join the rooms associated with this discord server using `/goanon` \n", ephemeral: true }
)
}
}
if (commandName === 'createroom') {
if (interaction.member.permissionsIn(interaction.channel).has(PermissionsBitField.Flags.Administrator)) {
const roomCount = await checkDiscordRoomCount(interaction);
if (roomCount.data.length <= 3) {
const roles = interaction.guild.roles.cache.map(role => {
return {
label: role.name,
value: `${role.name}: ${role.id}`
}
});
let roomName;
try {
roomName = interaction.options.getString('roomname') ? interaction.options.getString('roomname') : faker.location.streetAddress();
} catch {
roomName = `Room_${Date.now()}`
}
const selectRole = new StringSelectMenuBuilder()
.setCustomId('create-room')
.setPlaceholder('Select Role')
.setMinValues(1)
.setMaxValues(roles.length)
.addOptions(roles)
const row2 = new ActionRowBuilder()
.addComponents(selectRole)
await interaction.reply({ content: `Select the role(s) allowed to join ${roomName}`, components: [row2], ephemeral: true });
client.on('interactionCreate', async (interaction) => {
if (interaction.customId === 'create-room') {
const roleName = interaction.values[0].split(': ')[0];
const roleIds = interaction.values.map(role => role.split(': ')[1]);
const createdRoom = await createDiscordRoom(roomName);
const createdCode = await createClaimCode(interaction, [createdRoom.data.roomId])
await addRoleToRoom(roleIds, createdRoom.data.roomId, interaction)
await interaction.reply({ content: `You must claim this code before others can join your room \n Your code for ${roleName}: ${process.env.CLIENTURL}/signup/${createdCode.data.codes[0].claimcode}`, ephemeral: true });
}
})
} else {
await interaction.reply({ content: 'You have reached the maximum number of rooms for your server', ephemeral: true });
}
} else {
await interaction.reply({ content: 'You do not have permission to use this command', ephemeral: true });
}
}
if (commandName === 'addroletoroom') {
if (interaction.member.permissionsIn(interaction.channel).has(PermissionsBitField.Flags.Administrator)) {
const foundRooms = await findUserRooms(interaction);
const roomChoices = foundRooms.data.rooms.map(room => {
return {
label: room.name,
value: `${room.name}: ${room.roomId}`
}
})
if (roomChoices.length === 0) {
await interaction.reply({ content: 'You are not in any Discreetly rooms', ephemeral: true });
} else {
const selectRoom = new StringSelectMenuBuilder()
.setCustomId('rooms')
.setPlaceholder('Select Room')
.setMinValues(1)
.setMaxValues(1)
.addOptions(roomChoices)
const rows = new ActionRowBuilder()
.addComponents(selectRoom)
await interaction.reply({ content: 'Select the rooms you want to add to your server', components: [rows], ephemeral: true });
}
client.on('interactionCreate', async (interaction) => {
if (interaction.customId === 'rooms') {
const roomId = interaction.values[0].split(': ')[1]
const roomName = interaction.values[0].split(': ')[0]
const roles = interaction.guild.roles.cache.map(role => {
return {
label: role.name,
value: `${role.id}: ${roomId}`
}
});
const selectRole = new StringSelectMenuBuilder()
.setCustomId('roles')
.setPlaceholder(`Select Roles for ${roomName}`)
.setMinValues(1)
.setMaxValues(roles.length)
.addOptions(roles)
const rows = new ActionRowBuilder()
.addComponents(selectRole)
await interaction.reply({ content: `Select the roles you want to be associated with ${roomName}`, components: [rows], ephemeral: true });
client.on('interactionCreate', async (interaction) => {
if (interaction.customId === 'roles') {
const roleIds = interaction.values.map(role => role.split(': ')[0]);
const roomId = interaction.values[0].split(': ')[1];
await addRoleToRoom(roleIds, roomId, interaction);
await interaction.reply({ content: `Added ${roomName} to your server and users can now **/goanon** `, ephemeral: true });
}
})
}
})
} else {
await interaction.reply({ content: 'You do not have permission to use this command', ephemeral: true });
}
}
if (commandName === 'goanon') {
const interactionMember = interaction.member;
const roles = interactionMember.roles.cache.map(role => role.id);
let roomIdSet = new Set();
let roomNames = '';
try {
const foundRooms = await getUserRooms(roles, interaction);
if (foundRooms.data.rooms.length === 0) {
await interaction.reply({ content: 'No Discreetly rooms associated with this Discord Server', ephemeral: true });
}
foundRooms.data.rooms.forEach(room => {
roomIdSet.add(room);
})
foundRooms.data.roomNames.forEach((room, index) => {
if (index === foundRooms.data.roomNames.length - 1) {
roomNames += room;
} else {
roomNames += room + ', ';
}
})
} catch (error) {
console.error(error);
}
const roomIds = Array.from(roomIdSet);
const claimCode = await createClaimCode(interaction, roomIds)
await interaction.reply({ content: `An invite code lets you join a discreetly room only once with your username. \n **Please don't share it!** \n \n Code(s) for: **${roomNames}** \n \n Your invite link is ${process.env.CLIENTURL}/signup/${claimCode.data.codes[0].claimcode}`, ephemeral: true });
}
if (commandName === 'discreetlysay') {
if (interaction.member.permissionsIn(interaction.channel).has(PermissionsBitField.Flags.Administrator)) {
const message = interaction.options.getString('message')
interaction.channel.send(message);
await interaction.reply({ content: 'Message sent', ephemeral: true });
} else {
await interaction.reply({ content: 'You do not have permission to use this command', ephemeral: true });
}
}
});
});
client.login(TOKEN);