Post: add --enable-mentions flag to !post

This commit is contained in:
Dragory 2019-02-17 16:02:16 +02:00
parent 6cfadb98fd
commit 30f34747d4

View file

@ -1,6 +1,6 @@
import { Plugin, decorators as d } from "knub"; import { Plugin, decorators as d } from "knub";
import { Channel, EmbedBase, Message, TextChannel } from "eris"; import { Channel, EmbedBase, Message, Role, TextChannel } from "eris";
import { errorMessage, downloadFile } from "../utils"; import { errorMessage, downloadFile, roleMentionRegex, getRoleMentions } from "../utils";
import { GuildSavedMessages } from "../data/GuildSavedMessages"; import { GuildSavedMessages } from "../data/GuildSavedMessages";
import fs from "fs"; import fs from "fs";
@ -43,15 +43,22 @@ export class PostPlugin extends Plugin {
/** /**
* COMMAND: Post a message as the bot to the specified channel * COMMAND: Post a message as the bot to the specified channel
*/ */
@d.command("post", "<channel:channel> [content:string$]") @d.command("post", "<channel:channel> [content:string$]", {
options: [
{
name: "enable-mentions",
type: "bool",
},
],
})
@d.permission("post") @d.permission("post")
async postCmd(msg: Message, args: { channel: Channel; content?: string }) { async postCmd(msg: Message, args: { channel: Channel; content?: string; "enable-mentions": boolean }) {
if (!(args.channel instanceof TextChannel)) { if (!(args.channel instanceof TextChannel)) {
msg.channel.createMessage(errorMessage("Channel is not a text channel")); msg.channel.createMessage(errorMessage("Channel is not a text channel"));
return; return;
} }
const content = (args.content && this.formatContent(args.content)) || undefined; const content: string = (args.content && this.formatContent(args.content)) || undefined;
let downloadedAttachment; let downloadedAttachment;
let file; let file;
@ -68,9 +75,31 @@ export class PostPlugin extends Plugin {
return; return;
} }
const rolesMadeMentionable: Role[] = [];
if (args["enable-mentions"] && content) {
const mentionedRoleIds = getRoleMentions(content);
if (mentionedRoleIds != null) {
for (const roleId of mentionedRoleIds) {
const role = this.guild.roles.get(roleId);
if (role && !role.mentionable) {
await role.edit({
mentionable: true,
});
rolesMadeMentionable.push(role);
}
}
}
}
const createdMsg = await args.channel.createMessage(content, file); const createdMsg = await args.channel.createMessage(content, file);
await this.savedMessages.setPermanent(createdMsg.id); await this.savedMessages.setPermanent(createdMsg.id);
for (const role of rolesMadeMentionable) {
role.edit({
mentionable: false,
});
}
if (downloadedAttachment) { if (downloadedAttachment) {
downloadedAttachment.deleteFn(); downloadedAttachment.deleteFn();
} }