-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
177 lines (143 loc) · 8.07 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
171
172
173
174
175
176
177
// Load up the discord.js library
const Discord = require("discord.js");
// This is your client. Some people call it `bot`, some people call it `self`,
// some might call it `cootchie`. Either way, when you see `client.something`, or `bot.something`,
// this is what we're refering to. Your client.
const client = new Discord.Client();
// Here we load the config.json file that contains our token and our prefix values. (Mine is hosted with heroku so i commented this line)
//const config = require("./auth.json");
// config.token contains the bot's token
// config.prefix contains the message prefix.
client.on("ready", () => {
// This event will run if the bot starts, and logs in, successfully.
console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`);
// Example of changing the bot's playing game to something useful. `client.user` is what the
// docs refer to as the "ClientUser".
client.user.setActivity(`on ${client.guilds.size} servers`, {type: "STREAMING"});
});
client.on("guildCreate", guild => {
// This event triggers when the bot joins a guild.
console.log(`New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`);
client.user.setActivity(`on ${client.guilds.size} servers`, {type: "STREAMING"});
});
client.on("guildDelete", guild => {
// this event triggers when the bot is removed from a guild.
console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`);
client.user.setActivity(`on ${client.guilds.size} servers`, {type: "STREAMING"});
});
client.on("message", async message => {
// This event will run on every single message received, from any channel or DM.
// It's good practice to ignore other bots. This also makes your bot ignore itself
// and not get into a spam loop (we call that "botception").
if(message.author.bot) return;
// Also good practice to ignore any message that does not start with our prefix,
// which is set in the configuration file.
if(message.content.indexOf(process.env.prefix) !== 0) return;
// Here we separate our "command" name, and our "arguments" for the command.
// e.g. if we have the message "+say Is this the real life?" , we'll get the following:
// command = say
// args = ["Is", "this", "the", "real", "life?"]
const args = message.content.slice(process.env.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
// Let's go with a few common example commands! Feel free to delete or change those.
if(command === "rip") {
const attachment = new Discord.Attachment('https://i.imgur.com/w3duR07.png');
// Send the attachment in the message channel
message.channel.send(attachment);
}
if(command === "ava") {
message.channel.send(message.author.avatarURL);
}
if(command === "kick") {
// This command must be limited to mods and admins.
// Please read on Array.some() to understand this bit:
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some?
if(!message.member.hasPermission('KICK_MEMBERS') )
return message.reply("Sorry, you don't have permissions to use this!");
// Let's first check if we have a member and if we can kick them!
// message.mentions.members is a collection of people that have been mentioned, as GuildMembers.
// We can also support getting the member by ID, which would be args[0]
let member = message.mentions.members.first() || message.guild.members.get(args[0]);
if(!member)
return message.reply("Please mention a valid member of this server");
if(!member.kickable)
return message.reply("I cannot kick this user! Do they have a higher role? Do I have kick permissions?");
// slice(1) removes the first part, which here should be the user mention or ID
// join(' ') takes all the various parts to make it a single string.
let reason = args.slice(1).join(' ');
if(!reason) reason = "No reason provided";
// Now, time for a swift kick in the nuts!
await member.kick(reason)
.catch(error => message.reply(`Sorry ${message.author} I couldn't kick because of : ${error}`));
message.reply(`${member.user.tag} has been kicked by ${message.author.tag} because: ${reason}`);
}
if(command === "ban") {
// Most of this command is identical to kick, except that the permission is another.
if(!message.member.hasPermission('BAN_MEMBERS') )
return message.reply("Sorry, you don't have permissions to use this!");
let member = message.mentions.members.first();
if(!member)
return message.reply("Please mention a valid member of this server");
if(!member.bannable)
return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");
let reason = args.slice(1).join(' ');
if(!reason) reason = "No reason provided";
await member.ban(reason)
.catch(error => message.reply(`Sorry ${message.author} I couldn't ban because of : ${error}`));
message.reply(`${member.user.tag} has been banned by ${message.author.tag} because: ${reason}`);
}
if(command === "purge") {
// This command removes all messages from all users in the channel, up to 100.
// get the delete count, as an actual number.
if(!message.member.hasPermission('MANAGE_MESSAGES'))
return message.reply("Sorry, you don't have permissions to use this!");
const deleteCount = parseInt(args[0], 10);
if(!deleteCount || deleteCount < 2 || deleteCount > 100)
return message.reply("Please provide a number between 2 and 100 for the number of messages to delete");
// So we get our messages, and delete them.
const fetched = await message.channel.fetchMessages({limit: deleteCount});
message.channel.bulkDelete(fetched)
.catch(error => message.reply(`Couldn't delete messages because of: ${error}`));
}
if(command === "hello") {
const delay = ms => new Promise(res => setTimeout(res, ms));
message.channel.send("Hello Brudda")
}
//This is a help page shown by the bot.
if(command === "help") {
const embed = new Discord.RichEmbed()
.setTitle('Help')
.setColor(0xFF0000)
.setDescription('e!ping, show the current connection status. \n e!say, takes your text and sends it as ExoBot. \n e!kick, Kicks a User (only usable as Administrator or Moderator). \n e!ban, Bans a User (only usable as Administrator or Moderator). \n e!purge, X deletes all the lines before. X stays for 1 to 100 lines. \n e!hello try it :) \n e!rip \n e!ava shows your current avatar \n');
message.channel.send(embed);
}
if(command === "ping") {
// Calculates ping between sending a message and editing it, giving a nice round-trip latency.
// The second ping is an average latency between the bot and the websocket server (one-way, not round-trip)
const m = await message.channel.send("Ping?");
m.edit(`Pong! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms`);
}
if(command === "say") {
// makes the bot say something and delete the message. As an example, it's open to anyone to use.
// To get the "message" itself we join the `args` back into a string with spaces:
const sayMessage = args.join(" ");
// Then we delete the command message (sneaky, right?). The catch just ignores the error with a cute smiley thing.
message.delete().catch(O_o=>{});
// And we get the bot to say the thing:
message.channel.send(sayMessage);
}
if(command === "play") {
const delay = ms => new Promise(res => setTimeout(res, ms));
message.channel.send("This is fucked up, use Sandros command instead.")
}
//The Bot answers in a direct message.
client.on('message', msg => {
if (msg.channel.type == "dm") {
msg.author.send("I am OrionExodus#1636 Servant, pls don't DM me!")
.then(message => console.log(`Sent message: ${message.content}`))
.catch(console.error);
return;
}
});
});
client.login(process.env.token);