Add !newthread

master
Dragory 2018-04-08 02:56:30 +03:00
parent 4fd0929106
commit 4305609f0b
3 changed files with 50 additions and 11 deletions

View File

@ -40,10 +40,11 @@ async function findOpenThreadByUserId(userId) {
/** /**
* Creates a new modmail thread for the specified user * Creates a new modmail thread for the specified user
* @param {Eris.User} user * @param {Eris.User} user
* @param {Boolean} quiet If true, doesn't ping mentionRole or reply with responseMessage
* @returns {Promise<Thread>} * @returns {Promise<Thread>}
* @throws {Error} * @throws {Error}
*/ */
async function createNewThreadForUser(user) { async function createNewThreadForUser(user, quiet = false) {
const existingThread = await findOpenThreadByUserId(user.id); const existingThread = await findOpenThreadByUserId(user.id);
if (existingThread) { if (existingThread) {
throw new Error('Attempted to create a new thread for a user with an existing open thread!'); throw new Error('Attempted to create a new thread for a user with an existing open thread!');
@ -79,17 +80,19 @@ async function createNewThreadForUser(user) {
const newThread = await findById(newThreadId); const newThread = await findById(newThreadId);
// Ping moderators of the new thread if (! quiet) {
if (config.mentionRole) { // Ping moderators of the new thread
await newThread.postNonLogMessage({ if (config.mentionRole) {
content: `${utils.getInboxMention()}New modmail thread (${newThread.user_name})`, await newThread.postNonLogMessage({
disableEveryone: false content: `${utils.getInboxMention()}New modmail thread (${newThread.user_name})`,
}); disableEveryone: false
} });
}
// Send auto-reply to the user // Send auto-reply to the user
if (config.responseMessage) { if (config.responseMessage) {
newThread.postToUser(config.responseMessage); newThread.postToUser(config.responseMessage);
}
} }
// Post some info to the beginning of the new thread // Post some info to the beginning of the new thread

View File

@ -18,6 +18,8 @@ const webserver = require('./modules/webserver');
const greeting = require('./modules/greeting'); const greeting = require('./modules/greeting');
const typingProxy = require('./modules/typingProxy'); const typingProxy = require('./modules/typingProxy');
const version = require('./modules/version'); const version = require('./modules/version');
const newthread = require('./modules/newthread');
const attachments = require("./data/attachments"); const attachments = require("./data/attachments");
const {ACCIDENTAL_THREAD_MESSAGES} = require('./data/constants'); const {ACCIDENTAL_THREAD_MESSAGES} = require('./data/constants');
@ -169,6 +171,7 @@ module.exports = {
await webserver(bot); await webserver(bot);
await typingProxy(bot); await typingProxy(bot);
await version(bot); await version(bot);
await newthread(bot);
// Connect to Discord // Connect to Discord
console.log('Connecting to Discord...'); console.log('Connecting to Discord...');

33
src/modules/newthread.js Normal file
View File

@ -0,0 +1,33 @@
const utils = require("../utils");
const threadUtils = require("../threadUtils");
const threads = require("../data/threads");
module.exports = bot => {
const addInboxServerCommand = (...args) => threadUtils.addInboxServerCommand(bot, ...args);
addInboxServerCommand('newthread', async (msg, args, thread) => {
if (args.length === 0) return;
const userId = utils.getUserMention(args[0]);
if (! userId) return;
const user = bot.users.get(userId);
if (! user) {
utils.postSystemMessageWithFallback(msg.channel, thread, 'User not found!');
return;
}
const existingThread = await threads.findOpenThreadByUserId(user.id);
if (existingThread) {
utils.postSystemMessageWithFallback(msg.channel, thread, `Cannot create a new thread; there is another open thread with this user: <#${existingThread.channel_id}>`);
return;
}
const createdThread = await threads.createNewThreadForUser(user, true);
createdThread.postSystemMessage(`Thread was opened by ${msg.author.username}#${msg.author.discriminator}`);
if (thread) {
msg.delete();
}
});
};