From b7f06c18d464f6ecbde35c6970e13dda7376e9a5 Mon Sep 17 00:00:00 2001 From: Dragory Date: Sat, 14 Jul 2018 20:54:48 +0300 Subject: [PATCH] Add post plugin for posting messages as the bot --- src/index.ts | 4 ++- src/plugins/Post.ts | 59 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/plugins/Post.ts diff --git a/src/index.ts b/src/index.ts index 2908e2ff..79f6761d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,7 @@ import { BotControlPlugin } from "./plugins/BotControl"; import { ModActionsPlugin } from "./plugins/ModActions"; import { UtilityPlugin } from "./plugins/Utility"; import { LogsPlugin } from "./plugins/Logs"; +import { PostPlugin } from "./plugins/Post"; import knex from "./knex"; // Run latest database migrations @@ -28,7 +29,8 @@ knex.migrate.latest().then(() => { plugins: { utility: UtilityPlugin, mod_actions: ModActionsPlugin, - logs: LogsPlugin + logs: LogsPlugin, + post: PostPlugin }, globalPlugins: { bot_control: BotControlPlugin diff --git a/src/plugins/Post.ts b/src/plugins/Post.ts new file mode 100644 index 00000000..aab78f94 --- /dev/null +++ b/src/plugins/Post.ts @@ -0,0 +1,59 @@ +import { Plugin, decorators as d } from "knub"; +import { Channel, Message, TextChannel } from "eris"; +import { errorMessage } from "../utils"; + +export class PostPlugin extends Plugin { + getDefaultOptions() { + return { + permissions: { + post: false, + edit: false + }, + + overrides: [ + { + level: ">=100", + permissions: { + post: true, + edit: true + } + } + ] + }; + } + + /** + * Post a message as the bot to the specified channel + */ + @d.command("post", " ") + @d.permission("post") + async postCmd(msg: Message, args: { channel: Channel; content: string }) { + if (!(args.channel instanceof TextChannel)) { + msg.channel.createMessage(errorMessage("Channel is not a text channel")); + return; + } + + args.channel.createMessage(args.content); + } + + /** + * Edit the specified message posted by the bot + */ + @d.command("edit", " ") + @d.permission("edit") + async editCmd(msg, args) { + const message = await this.bot.getMessage(args.channel.id, args.messageId); + + if (!message) { + args.channel.createMessage(errorMessage("Unknown message")); + return; + } + + if (message.author.id !== this.bot.user.id) { + args.channel.createMessage(errorMessage("Message wasn't posted by me")); + return; + } + + message.edit(args.content); + } +}