Discord lol

This commit is contained in:
Flummi
2018-02-27 09:24:32 +01:00
parent 674ffeecf5
commit e99f318125
5 changed files with 110 additions and 3 deletions

97
src/clients/discord.mjs Normal file
View File

@@ -0,0 +1,97 @@
import { logger } from "../inc/log";
import { getLevel } from "../inc/admin";
import Discord from "discord.js";
import EventEmitter from "events";
export class discord extends EventEmitter {
constructor(options) {
super();
this.options = options || {};
this.token = options.token || null;
this.bot = new Discord.Client();
this.bot.login(this.token);
this.bot.on("message", msg => {
this.emit("data", ["message", this.reply(msg)]);
});
}
reply(tmp) {
return {
type: "discord",
network: "Discord",
channel: tmp.channel.name,
channelid: tmp.channel.id,
user: {
prefix: `${tmp.author.username}!${tmp.author.id}`,
nick: tmp.author.username,
username: tmp.author.username,
account: tmp.author.id.toString(),
level: getLevel("Discord", {
prefix: `${tmp.author.username}!${tmp.author.id}`,
nick: tmp.author.username,
username: tmp.author.username,
account: tmp.author.id.toString()
})
},
message: tmp.content,
time: ~~(Date.now() / 1000),
//raw: tmp,
reply: msg => this.send(tmp, this.format(msg)),
replyAction: msg => this.send(tmp, this.format(`*${msg}*`), "normal"),
replyNotice: msg => this.send(tmp, this.format(msg))
};
}
send(r, msg, mode="blah") {
switch(mode) {
case "normal":
r.channel.send(msg);
break;
default:
r.reply(msg);
break;
}
}
format(msg) {
return msg.toString()
.replace(/\[b\](.*?)\[\/b\]/g, "**$1**") // bold
.replace(/\[i\](.*?)\[\/i\]/g, "*$1*") // italic
.replace(/\[color=(.*?)](.*?)\[\/color\]/g, "$2")
;
}
};
/*
Channel:
msg.channel.id
msg.channel.name
Server:
msg.channel.guild.id
msg.channel.guild.name
User:
msg.author.id
msg.author.username
Message:
msg.content (msg.type === "DEFAULT")
*/
Map.prototype.hasi = function(val) {
for (let [key] of this)
if(key.toLowerCase() === val.toLowerCase())
return true;
return false;
};
Map.prototype.geti = function(val) {
for (let [key, value] of this)
if(key.toLowerCase() === val.toLowerCase())
return value;
return false;
};
Map.prototype.deli = function(val) {
for (let [key] of this)
if(key.toLowerCase() === val.toLowerCase())
this.delete(key);
};