-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
156 lines (150 loc) · 4.5 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
const { Client, Intents } = require('discord.js');
require('dotenv').config();
const fetchUser = require('./services/user');
const initCommands = require('./commands');
const { showContest } = require('./services/contest');
const { getPresentContests } = require('./services/cspresent');
const { getFutureContests } = require('./services/csupcoming');
const { PREVILAGE_TO_GIVE_ROLES } = require('./helper/constants');
const { createPoll } = require('./services/poll');
const fetchTopCoders = require('./services/topCodersContest');
const fetchTopUsers = require('./services/topuser');
const helpCommand = require('./services/help');
const {log,error} = require('./config/winston');
// const GENERAL_CHANNEL = "994182456913702924"
const GENERAL_CHANNEL = "1040153470755409921"
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
partials: ['MESSAGE']
});
client.on('ready', () => {
log.info(`Logged in as ${client.user.tag}!`);
const guild = client.guilds.cache.get(GENERAL_CHANNEL);
let commands
if (guild) {
commands = guild.commands
}
else {
commands = client.application.commands
}
initCommands(commands)
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName, options } = interaction;
if (commandName === 'user') {
const username = options.getString('username');
const user = await fetchUser(username);
interaction.reply({
embeds: [user],
})
}
else if (commandName === 'topusercontest') {
const contestCode = options.getString('contest');
const limit = options.getNumber('limit');
const topCoders = await fetchTopCoders(contestCode, limit);
interaction.reply({
embeds: [topCoders],
})
}
else if (commandName === 'topuser') {
const limit = options.getNumber('limit');
const topUsers = await fetchTopUsers(limit);
interaction.reply({
embeds: [topUsers],
})
}
else if (commandName === "contest") {
const UpcomingContest = await showContest();
interaction.reply({
embeds: [UpcomingContest],
});
}
else if (commandName === "help") {
const help = helpCommand();
interaction.reply({
embeds: [help],
});
}
else if (commandName === "cspresent") {
try {
const presentCodechefContest = await getPresentContests();
interaction.reply({
embeds: [presentCodechefContest],
});
}
catch (err) {
error.error(err);
interaction.reply({
content: "Something went wrong",
});
}
}
else if (commandName === "csupcoming") {
try {
const futureCodechefContest = await getFutureContests();
interaction.reply({
embeds: [futureCodechefContest],
});
} catch (err) {
error.error(err);
interaction.reply({
content: "Something went wrong",
});
}
}
else if (commandName === "roles") {
try {
if (interaction.member.roles.cache.has(...PREVILAGE_TO_GIVE_ROLES)) {
const role = options.getRole('role')
const user = options.getUser('user')
const member = interaction.guild.members.cache.get(user.id)
log.info(member)
await member.roles.add(role)
interaction.reply({
content: `<@${user.id}> has been given the role @${role.name}`,
});
}
else {
interaction.reply({
content: "Oops you don't have permissions to do that!",
});
}
} catch (err) {
error.error(err);
interaction.reply({
content: "Something went wrong",
});
}
}
else if (commandName === "rmrole") {
try {
if (interaction.member.roles.cache.has(...PREVILAGE_TO_GIVE_ROLES)) {
const role = options.getRole('role')
const user = options.getUser('user')
const member = interaction.guild.members.cache.get(user.id)
log.info(member)
await member.roles.remove(role)
interaction.reply({
content: `<@${user.id}> has been remove the role @${role.name}`,
});
}
else {
interaction.reply({
content: "Oops you don't have permissions to do that!",
});
}
} catch (err) {
error.error(err);
interaction.reply({
content: "Something went wrong",
});
}
}
else if (commandName === "poll") {
const title = options.getString('title');
const option = options.getString('options').split(',');
await createPoll(title, option, interaction)
}
})
client.login(process.env.BOT_TOKEN);