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

View File

@ -4,12 +4,14 @@
"description": "Bot, kennste?",
"main": "bot.js",
"scripts": {
"start": "npm run rmlog && node --experimental-modules src/bot.mjs",
"start": "node --experimental-modules src/bot.mjs",
"rmlog": "rm logs/*.log || true"
},
"author": "Flummi & jkhsjdhjs",
"license": "WTFPL",
"dependencies": {
"discord.io": "^2.5.3",
"discord.js": "^11.3.0",
"jsdom": "^11.4.0",
"pg-promise": "^7.3.2",
"request": "^2.83.0",

View File

@ -59,7 +59,7 @@ function trigger(args) {
this.help = args.help || false;
this.level = args.level || 0;
this.active = args.hasOwnProperty("active") ? args.active : true;
this.clients = args.clients || ["irc", "tg"];
this.clients = args.clients || ["irc", "tg", "discord"];
this.f = args.f;
}

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);
};

View File

@ -3,7 +3,7 @@ import cleverbot from "./lib/cleverbot";
export default bot => {
bot._trigger.set("chatbot", new bot.trigger({
call: /.*uwe.*/i,
clients: ["tg"],
clients: ["tg", "discord"],
active: true,
f: e => {
const chat = e.message

View File

@ -1,6 +1,7 @@
import { cfg } from "./cfg";
import { irc as irclib } from "../clients/irc";
import { tg as tglib } from "../clients/tg";
import { discord as discordlib } from "../clients/discord";
import util from "util";
import EventEmitter from "events";
@ -25,6 +26,13 @@ const wrapper = function () {
client: new tglib(cfg.client[srv].val)
});
break;
case "discord":
clients.push({
name: "discord",
type: "discord",
client: new discordlib(cfg.client[srv].val)
});
break;
}
}
}