-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_bot.js
181 lines (150 loc) · 5.69 KB
/
my_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
//Written by Zenegrad / Kyle Murphy
//9-25-2019 last updated
const Discord = require('discord.js');
const client = new Discord.Client();
var assignedChannel;
var regexPattern = /(~\S+).(\S+).(.*)/;
if(client.guilds)
client.on('ready', () => {
console.log("Connected as " + client.user.tag);
});
//Calls this function when a message is sent on discord
client.on('message', (receivedMessage) => {
//Switch this to regex so it finds the key words.
let messageArguments = receivedMessage.content.match(regexPattern);
// Prevent bot from responding to its own messages
if (receivedMessage.author == client.user) {
return;
}
//Checks to make sure the message is sent in the assigned channel. (If there is one).
if(receivedMessage.channel == assignedChannel || assignedChannel == null){
try{
//Checks to make sure the message is actually a command so the bot doesnt break for reg messages.
if(receivedMessage.content.charAt(0) == "~"){
console.log(messageArguments);
console.log("first arg: ", messageArguments[1]);
//call the right heckin method
if(messageArguments[1].toLowerCase() == "~dm " || messageArguments[1].toLowerCase() == "~dm"){
dmCommand(receivedMessage);
}
else if(receivedMessage.content.substring(0, 14).toLowerCase() == "~assignchannel"){
assignChannel(receivedMessage);
}
else{
console.log("Invalid input: ", messageArguments[0]);
}
}
}
catch(error){
console.log("Some noob put a space between ~ and dm....");
receivedMessage.channel.send("Somethings not right... make sure you used the correct command format! ``~dm @role message``");
}
}
});
//Returns the role ID from a mention/ping
function getIdFromMention(mention){
//Role parse.
if(mention.startsWith('<@&') && mention.endsWith('>')){
mention = mention.slice(3,-1);
//@here @everyone parse.
if(mention.startsWith('!')){
mention = mention.slice(1);
}
return mention;
}
return null;
}
//Returns ID of pinged channels
function getChannelId(mention){
if(mention.startsWith('<#') && mention.endsWith('>')){
mention = mention.slice(2,-1);
return mention;
}
return null;
}
//Function that handles ~dm command (sends a DM to all members involved)
function dmCommand(receivedMessage){
var messageArguments = receivedMessage.content.split(" ");
var rolesListed = [];
var message = "";
console.log(messageArguments[0]);
//DM Command
if(messageArguments[0].toLowerCase() === "~dm"){
//makes sure the command has correct number of args
if(messageArguments.length >= 3){
//gets the @roles ready for parsing
for(var index = 0; index < messageArguments.length; index++){
if(messageArguments[index].startsWith('<@&')){
rolesListed.push(messageArguments[index]);
console.log("Roles listed: ", rolesListed);
}
//If it isnt a role and isnt the first command (~dm) then it has to be part of the message.
else if(index >= 2 && !messageArguments[index].startsWith('<@')){
message += messageArguments[index] + ' ';
}
}
//User[] of all the users with the role
var membersWithRole = [];
//Doesnt kill the bot if it hits an error.
try{
//Finds all users with the roles pinged.
for(var i = 0; i < rolesListed.length; i++){
try{
membersWithRole = membersWithRole.concat(receivedMessage.guild.roles.get(getIdFromMention(rolesListed[i])).members.map(m=>m.user));
}catch(error){
console.error(error);
}
}
//loops through membersWithRole and sends them a message, if blocked will send a message back in the channel saying who blocked the bot.
for(var i = 0; membersWithRole.length > i; i++){
let username = membersWithRole[i].username;
membersWithRole[i].send(message).catch(error => {
if(error.code === 50007){
receivedMessage.channel.send(username + " has blocked the bot.");
}
});
}
}catch(e){
console.error(e);
receivedMessage.channel.send("Somethings not right... make sure you used the correct command format! ``~dm @role message``");
}
}
}
}
function assignChannel(receivedMessage){
var channelArray;
var pingedChannel;
var channelCount = 0;
let messageArguments = receivedMessage.content.split(" ");
//checks prefixs
if(messageArguments[0].toLowerCase() == "~assignchannel"){
console.log("message args: " , messageArguments.length);
switch(messageArguments.length){
case 1:
assignedChannel = receivedMessage.channel;
break;
//If assignChannel command has a second paramater get the channel
case 2:
pingedChannel = getChannelId(messageArguments[1]);
try{
channelArray = receivedMessage.guild.channels;
}catch(error){
console.log(error);
}
//Gets all channels and looks for only text channels and which channel has the same ID as the pinged channed in recievedMessage.
channelArray.forEach(TextChannel => {
if(TextChannel.type == 'text'){
if(TextChannel.id == pingedChannel)
assignedChannel = TextChannel;
}
});
break;
}
receivedMessage.channel.send("Assigned " + assignedChannel.name + " for all future bot commands.");
}
}
// Get your bot's secret token from:
// https://discordapp.com/developers/applications/
// Click on your application -> Bot -> Token -> "Click to Reveal Token"
const bot_secret_token = "";
client.login(bot_secret_token);