-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
138 lines (126 loc) · 6.09 KB
/
index.js
File metadata and controls
138 lines (126 loc) · 6.09 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
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
require("dotenv").config();
const { DISCORD_TOKEN, DB_TOKEN } = process.env;
const { connect } = require('mongoose');
const { Client, Collection, Events, GatewayIntentBits, Partials, ActivityType } = require('discord.js');
const fs = require('fs');
const Nominations = require ('./src/statics/nominationsUtility');
const client = new Client({
presence: {
status: 'online',
afk: false,
activities: [{
name: "Currently being worked on",
//name: "`/help`",
type: "PLAYING",
}],
},
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildEmojisAndStickers,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageTyping,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildScheduledEvents,
GatewayIntentBits.AutoModerationConfiguration,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMessages
],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
client.commands = new Collection();
client.commandArray = [];
client.color = "yellow";
/** FUNCTIONS */
const functionFolders = fs.readdirSync(`./src/functions`);
for(const folder of functionFolders) {
const functionFiles = fs
.readdirSync(`./src/functions/${folder}`)
.filter(file => file.endsWith('.js'));
for(const file of functionFiles) require(`./src/functions/${folder}/${file}`)(client);
}
client.handleEvents();
client.handleCommands();
client.on(Events.MessageReactionAdd, async (reaction, user) => {
try {
if (!reaction.message.guild || user.bot) return;
if (reaction.partial) {
try {
await reaction.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message:', error);
return;
}
}
const messageAuthorIsBot = reaction.message.author.bot;
const reactionAuthorId = user.id;
const messageCommand = reaction.message.interaction && reaction.message.interaction.commandName;
const messageEmbedTitle = reaction.message.embeds && reaction.message.embeds && reaction.message.embeds[0] ? reaction.message.embeds[0].data.title : "";
if(messageCommand === "nominate" && messageEmbedTitle === "Nominations Vote" && messageAuthorIsBot) {
const messageId = reaction.message.id;
// Retrieve the nomination
const nominationMessage = await Nominations.findByMessageId(messageId);
const selfVote = (nominationMessage.memberId === reactionAuthorId || nominationMessage.nominatingId === reactionAuthorId);
if(nominationMessage && nominationMessage.expires > new Date()) {
if ((nominationMessage.votersYes.length > 0 && nominationMessage.votersYes.includes(reactionAuthorId))
|| (nominationMessage.votersNo.length > 0 && nominationMessage.votersNo.includes(reactionAuthorId))
|| (nominationMessage.votersUnsure.length > 0 && nominationMessage.votersUnsure.includes(reactionAuthorId))
|| selfVote) {
reaction.users.remove(user);
const channel = reaction.message.guild.channels.cache.get(reaction.message.channelId);
const msgContent = selfVote
? `You cannot vote on a nomination that you are a part of!`
: `${user}, you can only vote once on a nomination, kweh!`;
const sentMessage = await channel.send(`${msgContent}\n*This message will self-destruct in 5 seconds...*`);
setTimeout(() => {
sentMessage.delete();
}, 5000);
} else {
await Nominations.updateNominationScore(nominationMessage.messageId, reaction._emoji.name, reactionAuthorId);
}
} else {
const sentMessage = await channel.send(`Voting is closed for this nomination because it has expired.\n*This message will self-destruct in 8 seconds...*`);
setTimeout(() => {
sentMessage.delete();
}, 8000);
console.error(`Encountered an error during message reaction for ${messageEmbedTitle}`);
}
}
} catch (error) {
console.error("Encountered an error during message reaction: ", error)
}
});
client.on(Events.MessageReactionRemove, async (reaction, user) => {
try {
if (!reaction.message.guild || user.bot) return;
if (reaction.partial) {
try {
await reaction.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message:', error);
return;
}
}
const messageAuthorIsBot = reaction.message.author.bot;
const reactionAuthorId = user.id;
const messageCommand = reaction.message.interaction && reaction.message.interaction.commandName;
const messageEmbedTitle = reaction.message && reaction.message.embeds && reaction.message.embeds[0] ? reaction.message.embeds[0].data.title : "";
if(messageCommand === "nominate" && messageEmbedTitle === "Nominations Vote" && messageAuthorIsBot) {
const messageId = reaction.message.id;
const nominationMessage = await Nominations.findByMessageId(messageId);
if(nominationMessage && nominationMessage.expires > new Date()) {
await Nominations.updateNominationScore(nominationMessage.messageId, reaction._emoji.name, reactionAuthorId, true);
} else {
console.error(`Encountered an error during message reaction for ${messageEmbedTitle}`);
}
}
} catch (error) {
console.error("Encountered an error during message reaction removal: ", error)
}
});
client.login(DISCORD_TOKEN);
(async () => {
await connect(DB_TOKEN).catch(console.error);
})();