Skip to content
This repository was archived by the owner on Nov 22, 2020. It is now read-only.

Commit cb43b34

Browse files
committed
Start Command and more
1 parent 72a557f commit cb43b34

File tree

11 files changed

+182
-27
lines changed

11 files changed

+182
-27
lines changed

base/Command.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ class Command {
99
aliases = new Array(),
1010
permLevel = "User",
1111
cooldown = 3,
12-
args = false
12+
args = false,
13+
DMonly = false
1314
}) {
1415
this.client = client;
15-
this.conf = { enabled, guildOnly, aliases, permLevel, cooldown, args };
16+
this.conf = { enabled, guildOnly, aliases, permLevel, cooldown, args, DMonly };
1617
this.help = { name, description, category, usage };
1718
}
1819
}
19-
20-
module.exports = Command;
20+
21+
module.exports = Command;

base/emojis.json

-6
This file was deleted.

commands/ping.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Command = require("../base/Command");
22

3-
class PingCmd extends Command {
3+
class Ping extends Command {
44
constructor (client) {
55
super(client, {
66
name: "ping",
@@ -12,7 +12,8 @@ class PingCmd extends Command {
1212
aliases: [],
1313
permLevel: "User",
1414
cooldown: 5,
15-
args: false
15+
args: false,
16+
DMonly: false
1617
});
1718
}
1819

@@ -24,4 +25,4 @@ class PingCmd extends Command {
2425
}
2526
}
2627

27-
module.exports = PingCmd;
28+
module.exports = Ping;

commands/start.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const Command = require("../base/Command");
2+
const Discord = require("discord.js");
3+
const Thread = require("../models/thread.js");
4+
5+
class Start extends Command {
6+
constructor (client) {
7+
super(client, {
8+
name: "start",
9+
description: "Starts a new ticket via private messages.",
10+
category: "",
11+
usage: "Modmail",
12+
enabled: true,
13+
guildOnly: false,
14+
aliases: [],
15+
permLevel: "User",
16+
cooldown: 5,
17+
args: false,
18+
DMonly: true
19+
});
20+
}
21+
22+
async run (message, args, level, reply) { // eslint-disable-line no-unused-vars
23+
const haveThread = await Thread.findOne({ recipient: message.author.id, closed: false });
24+
if (haveThread) return reply("You already have an ongoing thread.");
25+
26+
const issueEmbed = new Discord.MessageEmbed()
27+
.setAuthor(message.author.tag, message.author.displayAvatarURL())
28+
.setDescription("Hello, please describe your issue briefly, the more details, the faster it will be for the staff team to help you. Type `cancel` to cancel.")
29+
.setColor("ORANGE");
30+
const issue = await this.client.awaitReply(message, issueEmbed, 900000);
31+
if (issue === false) return reply(`${this.client.config.emojis.redTick} Your request has timed out. If you wish to submit a request please use \`${this.client.config.prefix}start\`.`);
32+
if (issue.toLowerCase() === "cancel") return reply(this.client.config.emojis.greenTick + " Aborted.");
33+
34+
const channel = await this.client.staffGuild.channels.create(message.author.tag.replace("#", "-"), {
35+
type: "text",
36+
topic: `User: ${message.author.tag} (ID: ${message.author.id})\n\nIssue: ${issue}`,
37+
nsfw: false,
38+
parent: this.client.config.parent,
39+
permissionOverwrites: [{
40+
id: this.client.staffGuild.id,
41+
deny: ["VIEW_CHANNEL"],
42+
type: "role"
43+
},
44+
{
45+
id: this.client.config.supportRole,
46+
allow: ["VIEW_CHANNEL"],
47+
type: "role"
48+
}]
49+
});
50+
51+
const infoEmbed = new Discord.MessageEmbed()
52+
.setAuthor(message.author.tag, message.author.displayAvatarURL())
53+
.setDescription(`**${message.author.tag}** (ID: ${message.author.id}) has opened a ticket.\nAccount was created ${fetchTime(Date.now() - message.author.createdTimestamp)} ago.`)
54+
.addField("Issue:", issue)
55+
.setColor("ORANGE")
56+
.setTimestamp();
57+
await channel.send(infoEmbed);
58+
59+
var threadID = await Thread.countDocuments();
60+
threadID += 1;
61+
62+
await (new Thread({
63+
id: threadID,
64+
recipient: message.author.id,
65+
channel: channel.id,
66+
guild: channel.guild.id,
67+
issue: issue,
68+
timestamp: Date.now()
69+
}).save());
70+
}
71+
}
72+
73+
module.exports = Start;
74+
75+
function fetchTime (ms, object = false) {
76+
var totalSeconds = (ms / 1000);
77+
var days = Math.floor(totalSeconds / 86400);
78+
totalSeconds %= 86400;
79+
var hours = Math.floor(totalSeconds / 3600);
80+
totalSeconds %= 3600;
81+
var minutes = Math.floor(totalSeconds / 60);
82+
var seconds = totalSeconds % 60;
83+
seconds = Math.floor(seconds);
84+
if (object === true) return { days, hours, minutes, seconds };
85+
86+
return `${days} Days, ${hours} Hours, ${minutes} Minutes and ${seconds} Seconds`;
87+
}

config.js.txt

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ const config = {
44
"owner": "BOT_OWNER_ID",
55
"admins": [],
66
"mongo": "MONGODB_URL",
7+
"mainGuild": "GUILD_ID",
8+
"staffGuild": "GUILD_ID",
9+
"parent": "GUILD_CATEGORY_ID", // This should be inside staff's guild.
10+
"supportRole": "ROLE_ID",
711

812
"emojis": {
913
"redTick": "❌",

events/message.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ module.exports = class {
2323

2424
const level = await this.client.permlevel(message);
2525

26-
if (cmd.conf.enabled === false) return reply(this.client.config.emojis.redTick + " This command is currently globally disabled.");
26+
if (cmd && !message.guild && cmd.conf.guildOnly) return reply(this.client.config.emojis.redTick + "This command is unavailable via private message, please run this command in a server.");
27+
28+
if (cmd.conf.DMonly === true && message.guild) return reply(this.client.config.emojis.redTick + " This command can only be used in direct messages, please run this command in the private messages.");
29+
if (cmd.conf.enabled === false) return reply(this.client.config.emojis.redTick + " This command is disabled.");
2730

2831
if (cmd.conf.args === true && !args.length) {
2932
return reply(`${this.client.config.emojis.redTick} You haven't provided any arguments. Correct Usage: \`${this.client.config.prefix}${cmd.help.name} ${cmd.help.usage}\``);
@@ -53,17 +56,15 @@ module.exports = class {
5356
}
5457

5558

56-
if (cmd && !message.guild && cmd.conf.guildOnly) return message.channel.send(this.client.emojis.redTick + "This command is unavailable via private message.\n**Please run this command in a server.**");
57-
5859
if (level < this.client.levelCache[cmd.conf.permLevel]) return reply(`${this.client.config.emojis.redTick} You do not have the required permission to execute this command, the permission you need is \`${cmd.conf.permLevel}\`.`);
5960

60-
try {
61+
// try {
6162
message.channel.startTyping(100);
6263
await cmd.run(message, args, level, reply);
6364
message.channel.stopTyping(true);
64-
} catch (e) {
65-
reply(`${this.client.config.emojis.redTick} **Oops, seems like these was an error executing command. Please open an issue at https://github.com/MrAugu/modmail-js/issues.`);
66-
await message.channel.stopTyping(true);
67-
}
65+
// } catch (e) {
66+
// reply(`${this.client.config.emojis.redTick} **Oops, seems like these was an error executing command. Please open an issue at https://github.com/MrAugu/modmail-js/issues.`);
67+
// await message.channel.stopTyping(true);
68+
// }
6869
}
6970
};

events/ready.js

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ module.exports = class {
1111
await this.client.user.setStatus("online");
1212
await this.client.user.setActivity(`${this.client.guilds.size} Servers | ${this.client.config.prefix}help`, { type: "WATCHING" });
1313

14+
this.client.staffGuild = this.client.guilds.get(this.client.config.staffGuild);
15+
this.client.mainGuild = this.client.guilds.get(this.client.config.mainGuild);
16+
1417
this.client.logger.ready(`Logged in as ${this.client.user.tag}. Serving ${this.client.guilds.size} Servers and ${this.client.users.size} Users.`);
1518
}
1619
};

index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class Bot extends Client {
2828
const ms = await msg.channel.send(question);
2929
try {
3030
const collected = await msg.channel.awaitMessages(filter, { max: 1, time: limit, errors: ["time"] });
31-
await collected.first().delete();
32-
await ms.delete();
3331
return collected.first().content;
3432
} catch (e) {
3533
return false;
@@ -67,7 +65,7 @@ class Bot extends Client {
6765
loadCommand (commandPath, commandName) {
6866
try {
6967
const props = new (require(`${commandPath}${path.sep}${commandName}`))(this);
70-
this.logger.log(`Loading Command: ${props.help.name}. 👌`, "log");
68+
this.logger.log(`Loading Command: ${props.help.name}`, "log");
7169
props.conf.location = commandPath;
7270
if (props.init) {
7371
props.init(this);
@@ -78,6 +76,7 @@ class Bot extends Client {
7876
});
7977
return false;
8078
} catch (e) {
79+
throw e;
8180
return `Unable to load command ${commandName}: ${e}`;
8281
}
8382
}

models/message.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Schema = new mongoose.Schema({
77
channel: { type: String },
88
content: { type: String },
99
author: { type: String },
10+
attachments: { type: Array },
1011
timestamp: { type: Number }
1112
});
1213

models/thread.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ const Schema = new mongoose.Schema({
55
recipient: { type: String },
66
channel: { type: String },
77
guild: { type: String },
8-
subscribedRoles: { type: Array, default: null },
9-
subscribedUsers: { type: Array, default: null },
8+
issue: { type: String },
9+
subscribedRoles: { type: Array, default: [] },
10+
subscribedUsers: { type: Array, default: [] },
1011
nsfw: { type: Boolean, default: false },
11-
closed: { type: Boolean, default: false }
12+
closed: { type: Boolean, default: false },
13+
timestamp: { type: Number }
1214
});
1315

1416
module.exports = mongoose.model("threads", Schema);

modules/threading.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const Thread = require("../models/thread.js");
2+
const Message = require("../models/message.js");
3+
const Snippet = require("../models/snippet.js");
4+
const Discord = require("discord.js");
5+
6+
class Threading {
7+
static async message (client, message) {
8+
if (message.author.bot) return;
9+
10+
if (message.channel.type === "dm") {
11+
const recipientThread = await Thread.findOne({ recipient: message.author.id, closed: false });
12+
if (!recipientThread) return;
13+
14+
this.dm(client, message, recipientThread);
15+
} else if (message.chanenl.type === "text") {
16+
const recipientThread = await Thread.findOne({ channel: message.channel.id, closed: false });
17+
if (!recipientThread) return;
18+
19+
this.channel(client, message, recipientThread, message.content.startsWith("a:"));
20+
}
21+
}
22+
23+
static async dm (client, message, thread) {
24+
const channel = client.channels.get(thread.channel);
25+
if (!channel) console.log(`Channel for thread ${thread.id} can not be found.`);
26+
27+
const contentEmbed = new Discord.MessageEmbed()
28+
.setAuthor(message.author.tag, message.author.displayAvatarURL())
29+
.setDescription(message.content)
30+
.setColor("ORANGE")
31+
.setTimestamp();
32+
channel.send(contentEmbed).catch(e => {});
33+
34+
message.attachments.forEach(attachment => {
35+
const embed = new Discord.MessageEmbed()
36+
.setAuthor(message.author.tag, message.author.displayAvatarURL())
37+
.setColor("ORANGE")
38+
.setImage(attachment.proxyURL)
39+
.setTimestamp();
40+
channel.send(embed);
41+
});
42+
43+
const messageID = (await Message.countDocuments({ thread: thread.id })) + 1;
44+
45+
await (new Message({
46+
thread: thread.id,
47+
message: messageID,
48+
recipient: "Staff",
49+
channel: message.channel.id,
50+
content: message.content,
51+
author: message.author.id,
52+
attachments: message.attachments.map(a => a.proxyURL),
53+
timestamp: Date.now()
54+
}).save());
55+
}
56+
57+
static async channel (client, message, thread, isAnonymous) {
58+
// In case dm appears to come from a channel thread. (Support)
59+
}
60+
}
61+
62+
module.exports = Threading;

0 commit comments

Comments
 (0)