Skip to content

Commit 505f8ba

Browse files
committed
Command Handling
1 parent 8543c89 commit 505f8ba

3 files changed

Lines changed: 283 additions & 0 deletions

File tree

src/commands/command-admin.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = (WDR, Message) => {
2+
let command = Message.content.split(" ")[0].slice(1);
3+
try {
4+
if (WDR.Fs.existsSync(WDR.Dir + "/src/commands/admin/" + command.toLowerCase() + ".js")) {
5+
Message.delete();
6+
let Cmd = require(WDR.Dir + "/src/commands/admin/" + command.toLowerCase() + ".js");
7+
Cmd(WDR, Message);
8+
}
9+
} catch (error) {
10+
11+
}
12+
}

src/commands/command-channel.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
module.exports = (WDR, Message) => {
2+
3+
if (!Message.member) {
4+
return;
5+
}
6+
7+
Message.member.isAdmin = Message.member.hasPermission("ADMINISTRATOR") ? true : false;
8+
9+
Message.member.isMod = Message.member.hasPermission("MANAGE_ROLES") ? true : false;
10+
11+
WDR.Discords.forEach(async (Server, index) => {
12+
13+
if (Message.guild.id == Server.id && Server.command_channels.indexOf(Message.channel.id) >= 0) {
14+
15+
Message.Discord = Server;
16+
17+
// DELETE THE MESSAGE
18+
//if (!WDR.Config.Tidy_Channel || WDR.Config.Tidy_Channel == "ENABLED") {
19+
Message.delete();
20+
//}
21+
22+
// FETCH THE GUILD MEMBER AND CHECK IF A DONOR
23+
if (Message.member.isAdmin || Message.member.isMod) {
24+
/* DO NOTHING */
25+
} else if (Server.donor_role && !Message.member.roles.cache.has(Server.donor_role)) {
26+
if (WDR.Config.log_channel) {
27+
let nondonor_embed = new WDR.DiscordJS.MessageEmbed()
28+
.setColor("ff0000")
29+
.addField("User attempted to use a subsciption command, not a donor. ", Message.member.user.username);
30+
if (WDR.Config.donor_info) {
31+
Message.donor_info = WDR.Config.donor_info;
32+
} else {
33+
Message.donor_info = "";
34+
}
35+
Message.guild.members.fetch(Message.author.id).then(TARGET => {
36+
TARGET.send("This feature is only for donors. " + Message.donor_info).catch(console.error);
37+
});
38+
return WDR.Send_Embed(WDR, "member", 0, Server.id, "", nondonor_embed, WDR.Config.log_channel);
39+
} else {
40+
return console.log(WDR.Color.red + "User attempted to use DM command, not a donor. " + Message.member.user.username + WDR.Color.reset);
41+
}
42+
}
43+
44+
// LOAD DATABASE RECORD BASED OFF OF ORIGIN SERVER_ID AND AUTHOR_ID
45+
WDR.wdrDB.query(
46+
`SELECT
47+
*
48+
FROM
49+
wdr_users
50+
WHERE
51+
user_id = ${Message.member.id}
52+
AND guild_id = ${Message.guild.id};`,
53+
async function(error, user, fields) {
54+
if (!user || !user[0]) {
55+
Message.member.db = await WDR.Save_User(WDR, Message, Server);
56+
Message.reply("Created database record for you. Please repeat the previous command.").then(m => m.delete({
57+
timeout: 5000
58+
})).catch(console.error);
59+
} else {
60+
Message.member.db = user[0];
61+
62+
let command = Message.content.split(" ")[0].slice(1);
63+
switch (command) {
64+
case "p":
65+
command = "pokemon";
66+
break;
67+
case "r":
68+
command = "raid";
69+
break;
70+
case "q":
71+
command = "quest";
72+
break;
73+
case "l":
74+
command = "location";
75+
break;
76+
case "a":
77+
command = "area";
78+
break;
79+
}
80+
//try {
81+
if (WDR.Fs.existsSync(WDR.Dir + "/src/commands/subscription/" + command.toLowerCase() + "/begin.js")) {
82+
let Cmd = require(WDR.Dir + "/src/commands/subscription/" + command.toLowerCase() + "/begin.js");
83+
if (Cmd) {
84+
Cmd(WDR, Message);
85+
}
86+
} else if (WDR.Fs.existsSync(WDR.Dir + "/src/commands/subscription/" + command.toLowerCase() + ".js")) {
87+
let Cmd = require(WDR.Dir + "/src/commands/subscription/" + command.toLowerCase() + ".js");
88+
if (Cmd) {
89+
Cmd(WDR, Message);
90+
}
91+
} else {
92+
WDR.Console.error("[handlers/commands.js] " + Message.content + " command does not exist.");
93+
}
94+
// } catch (error) {
95+
// try {
96+
97+
// console.log("2", command.toLowerCase());
98+
// } catch (error) {
99+
// console.log("3", command.toLowerCase());
100+
// //WDR.Console.error("[handlers/commands.js] Error Initializing Command", [command, error]);
101+
// }
102+
// }
103+
}
104+
if (user.user_name != Message.member.user.username) {
105+
WDR.wdrDB.query(`
106+
UPDATE
107+
wdr_users
108+
SET
109+
user_name = '${Message.member.user.username}'
110+
WHERE
111+
user_id = ${Message.member.id};
112+
`);
113+
WDR.wdrDB.query(`
114+
UPDATE
115+
wdr_subscriptions
116+
SET
117+
user_name = '${Message.member.user.username}'
118+
WHERE
119+
user_id = ${Message.member.id};
120+
`);
121+
}
122+
123+
// END
124+
return;
125+
}
126+
);
127+
}
128+
});
129+
}

src/commands/command-dm.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
module.exports = (WDR, Message) => {
2+
3+
Message.author.user_guilds = [];
4+
5+
WDR.Discords.forEach(async (Server, index) => {
6+
let guild = WDR.Bot.guilds.cache.get(Server.id);
7+
if (!guild) {
8+
WDR.Console.error(WDR, "[handlers/commands.js] Guild ID `" + Server.id + "` found in the database that does not match any guilds in the config.");
9+
} else {
10+
let member = WDR.Bot.guilds.cache.get(Server.id).members.cache.get(Message.author.id);
11+
if (member && member.roles.cache.has(Server.donor_role)) {
12+
Message.author.user_guilds.push({
13+
id: guild.id,
14+
name: Server.name
15+
});
16+
}
17+
}
18+
});
19+
20+
if (Message.author.user_guilds.length == 1) {
21+
WDR.wdrDB.query(
22+
`SELECT
23+
*
24+
FROM
25+
users
26+
WHERE
27+
user_id = ${Message.author.id}
28+
AND guild_id = ${user_guilds[0]}`,
29+
async function(error, user, fields) {
30+
if (!user || !user[0]) {
31+
return Message.reply("Before you can create and modify subscriptions via DM, you must first use the subsciption channel in your scanner discord.");
32+
} else {
33+
Message.author.db = user[0];
34+
}
35+
36+
let command = Message.content.split(" ")[0].slice(1);
37+
switch (command) {
38+
case "p":
39+
command = "pokemon";
40+
break;
41+
case "r":
42+
command = "raid";
43+
break;
44+
case "q":
45+
command = "quest";
46+
break;
47+
}
48+
49+
let Cmd = require(WDR.Dir + "/src/commands/subscription/" + command.toLowerCase() + "/begin.js")
50+
if (Cmd) {
51+
Cmd(WDR, Message);
52+
}
53+
}
54+
);
55+
} else if (Message.author.user_guilds.length > 1) {
56+
57+
let list = "";
58+
Message.author.user_guilds.forEach((guild, i) => {
59+
list += (i + 1) + " - " + guild.name + "\n";
60+
});
61+
list = list.slice(0, -1);
62+
63+
let request_action = new WDR.DiscordJS.MessageEmbed()
64+
.setAuthor(Message.author.username, Message.author.displayAvatarURL())
65+
.setTitle("Which Discord would you like to modify Subscriptions for?")
66+
.setDescription(list)
67+
.setFooter("Type the number of the discord.");
68+
69+
Message.channel.send(request_action).catch(console.error).then(BotMsg => {
70+
71+
const filter = CollectedMsg => CollectedMsg.author.id == Message.author.id;
72+
const collector = Message.channel.createMessageCollector(filter, {
73+
time: 60000
74+
});
75+
76+
collector.on("collect", CollectedMsg => {
77+
78+
let num = parseInt(CollectedMsg.content);
79+
80+
switch (true) {
81+
case (isNaN(CollectedMsg.content)):
82+
return CollectedMsg.reply("`" + CollectedMsg.content + "` is not a Number. Type the number next to the Discord name above.").then(m => m.delete({
83+
timeout: 5000
84+
})).catch(console.error);
85+
case (num > 0 && num <= list.length):
86+
return collector.stop((num - 1));
87+
default:
88+
return CollectedMsg.reply("`" + CollectedMsg.content + "` is not a valid # selection. Type the number next to the Discord name above.").then(m => m.delete({
89+
timeout: 5000
90+
})).catch(console.error);
91+
}
92+
});
93+
94+
collector.on("end", (collected, num) => {
95+
96+
console.log(Message.author.user_guilds);
97+
console.log("num", Message.author.user_guilds[num])
98+
99+
BotMsg.delete();
100+
let query = `
101+
SELECT
102+
*
103+
FROM
104+
wdr_users
105+
WHERE
106+
user_id = ${Message.author.id}
107+
AND guild_id = ${Message.author.user_guilds[num].id};
108+
`;
109+
console.log(query);
110+
WDR.wdrDB.query(
111+
query,
112+
async function(error, user, fields) {
113+
if (error) {
114+
WDR.Console.error(WDR, "[src/handlers/commands.js] DM: Error Fetching User From DB", [query, error])
115+
}
116+
Message.author.db = user[0];
117+
118+
let command = Message.content.split(" ")[0].slice(1);
119+
switch (command) {
120+
case "p":
121+
command = "pokemon";
122+
break;
123+
case "r":
124+
command = "raid";
125+
break;
126+
case "q":
127+
command = "quest";
128+
break;
129+
}
130+
131+
let Cmd = require(WDR.Dir + "/src/commands/subscription/" + command.toLowerCase() + "/begin.js")
132+
if (Cmd) {
133+
Cmd(WDR, Message);
134+
}
135+
}
136+
);
137+
});
138+
});
139+
} else {
140+
return Message.reply("I did not find any Discords in which you are a Donor. Please go to your discord's subscribe website before modifying subscriptions");
141+
}
142+
}

0 commit comments

Comments
 (0)