Skip to content

Instantly share code, notes, and snippets.

@bonzaiferroni
Created September 7, 2018 15:34
Show Gist options
  • Save bonzaiferroni/016b417cdf8cfc4596162ce19a358b29 to your computer and use it in GitHub Desktop.
Save bonzaiferroni/016b417cdf8cfc4596162ce19a358b29 to your computer and use it in GitHub Desktop.
BotBot example module
import {BotModule, ModuleMemory} from "./BotModule";
import {HelpData} from "./HelpModule";
import {BotMessage} from "../BotMessage";
import {Client, Message, MessageReaction, TextChannel} from "discord.js";
import {EmojiHelper} from "../EmojiHelper";
export class BookmarkModule extends BotModule<BookmarkMemory> {
protected id = "bookmarks";
helpData: HelpData = {
description: "Designate bookmark channel",
command: "!bookmarks",
};
constructor(client: Client) {
super(client);
if (!this.memory.channelIds) this.memory.channelIds = {};
}
public onMessage(msg: BotMessage) {
this.saveBookmarkChannel(msg);
}
private saveBookmarkChannel(msg: BotMessage) {
if (!msg.commands.bookmarks) return;
msg.consumed = true;
if (!(msg.channel instanceof TextChannel)) return;
this.memory.channelIds[msg.server.id] = msg.channel.id;
this.save();
msg.react(EmojiHelper.thumbsUp);
}
public onReact(reaction: MessageReaction) {
this.addBookmark(reaction);
this.removeBookmark(reaction);
}
private removeBookmark(reaction: MessageReaction) {
if (reaction.emoji.name !== EmojiHelper.crossmark) return;
// don't respond to first reaction (which was botbot)
if (reaction.count < 2) return;
// don't remove from other channels (although we should generalize this functionality)
if (reaction.message.channel.id !== this.memory.channelIds[reaction.message.guild.id]) return;
reaction.message.delete();
}
private addBookmark(reaction: MessageReaction) {
if (reaction.emoji.name !== EmojiHelper.bookmark) return;
// only respond to first bookmark
if (reaction.count > 1) return;
// don't respond if the server doesn't have a !bookmark channel
let channelId = this.memory.channelIds[reaction.message.guild.id];
if (!channelId) return;
// don't pin anything from the bookmark channel
if (channelId === reaction.message.channel.id) return;
let originalChannel = reaction.message.channel as TextChannel;
let channel = this.client.channels.get(channelId) as TextChannel;
channel.send(`${reaction.message.content}\n\n*Posted by ${reaction.message.author.username} in #${originalChannel.name}*`)
.then(myMsg => {
if (!(myMsg instanceof Message)) return;
myMsg.react(EmojiHelper.clap);
}).catch(e => console.log(e));
}
}
interface BookmarkMemory extends ModuleMemory {
channelIds: {[guildId: string]: string}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment