use classes for chat-clients

This commit is contained in:
BuckarooBanzay 2021-07-02 07:51:14 +02:00
parent b33bead9f8
commit e36bce5a86
3 changed files with 33 additions and 33 deletions

View File

@ -1,36 +1,35 @@
const Discord = require('discord.js');
let client;
module.exports = {
destroy: function(){
client.destroy();
},
init: function(remote, events){
client = new Discord.Client({
module.exports = class {
destroy(){
this.client.destroy();
}
init(remote, events){
this.client = new Discord.Client({
autoReconnect: true
});
client.on('ready', e => {
this.client.on('ready', e => {
console.error("error", e);
});
client.on('disconnect', e => {
this.client.on('disconnect', e => {
console.error("disconnect", e);
});
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
this.client.on('ready', () => {
console.log(`Logged in as ${this.client.user.tag}!`);
// Send connection announcement
Object.keys(remote.channels).forEach(ingame_name => {
const discord_channel_name = remote.channels[ingame_name];
const channel = client.channels.cache.find(ch => ch.name === discord_channel_name);
const channel = this.client.channels.cache.find(ch => ch.name === discord_channel_name);
if (channel) {
channel.send(`beerchat_proxy connected! ingame-channel: ${ingame_name}`);
}
});
events.on("message-out", function(event){
events.on("message-out", (event) => {
if (event.name == remote.name)
// not meant for this remote, ignore
// source == destination
@ -49,7 +48,7 @@ module.exports = {
if (event.target_username != null){
// send PM to discord user
const user = client.users.cache.find(u => u.username == event.target_username);
const user = this.client.users.cache.find(u => u.username == event.target_username);
if (user != null && event.message != ""){
user.send(event.message);
}
@ -68,7 +67,7 @@ module.exports = {
return;
}
const channel = client.channels.cache.find(ch => ch.name == discord_channel_name);
const channel = this.client.channels.cache.find(ch => ch.name == discord_channel_name);
if (channel) {
let message = "";
@ -99,7 +98,7 @@ module.exports = {
});
});
client.on('message', msg => {
this.client.on('message', msg => {
if (msg.author.bot){
// ignore other bots
return;
@ -136,7 +135,7 @@ module.exports = {
}
});
client.login(remote.token)
this.client.login(remote.token)
.catch(e => console.error("login", e));
}
};

View File

@ -1,13 +1,13 @@
const IRC = require('irc-framework');
let client;
module.exports = {
destroy: function(){
client.quit("bye o/");
},
init: function(remote, events){
client = new IRC.Client();
client.connect({
module.exports = class {
destroy() {
this.client.quit("bye o/");
}
init(remote, events) {
this.client = new IRC.Client();
this.client.connect({
host: remote.host,
port: remote.port,
nick: remote.username,
@ -18,7 +18,7 @@ module.exports = {
});
if (remote.debug) {
client.on("debug", function(e){
this.client.on("debug", function(e){
console.log(e);
});
}
@ -26,12 +26,12 @@ module.exports = {
var channels = {}; // name -> channelObj
// map channels
client.on('registered', function() {
this.client.on('registered', () => {
let delay = 5000;
Object.keys(remote.channels).forEach(ingame_name => {
setTimeout(function(){
const irc_name = remote.channels[ingame_name];
var channel = client.channel("#" + irc_name);
var channel = this.client.channel("#" + irc_name);
channel.join();
channel.say(`beerchat_proxy connected! ingame-channel: ${ingame_name}`);
channels[ingame_name] = channel;
@ -40,7 +40,7 @@ module.exports = {
});
});
client.on('message', function(event) {
this.client.on('message', event => {
if (remote.debug){
console.log("irc-event-in", event);
}
@ -78,7 +78,7 @@ module.exports = {
});
});
events.on("message-out", function(event){
events.on("message-out", event => {
if (event.name == remote.name)
//not meant for this remote, ignore
return;
@ -96,7 +96,7 @@ module.exports = {
if (event.target_username != null){
// send PM to IRC user
client.say(event.target_username, event.message);
this.client.say(event.target_username, event.message);
} else if (event.channel != null) {
// channel name sent, map to config channels

View File

@ -20,10 +20,11 @@ const handlers = {
};
cfg.remotes.forEach(remote => {
const handler = handlers[remote.type];
const Handler = handlers[remote.type];
const instance = new Handler();
console.log(`Setting up remote: ${remote.name} with type: ${remote.type}`);
handler.init(remote, events);
instance.init(remote, events);
});
events.on("reconnect", function(){