-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
155 lines (126 loc) · 4.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const IRC = require('irc-framework');
const { Client, GatewayIntentBits } = require('discord.js');
const fs = require('fs');
let bots = {};
let channels = {};
const config = JSON.parse(fs.readFileSync('config.json'));
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers
],
});
client.login(config.discordToken);
client.on('ready', () => {
console.log(`Bot is ready!`);
Object.values(config.channels).forEach(channelId => {
channels[channelId] = client.channels.cache.get(channelId);
});
bots[client.user.id] = createBot(config.nick, 'relay', () => {
bots[client.user.id].on('message', function (event) {
if(config.excludeNicks.includes(event.nick))
return;
let channel = channels[config.channels[event.target]];
if (!messageIsFromRelay(event.nick)) {
event.nick = stripColours(event.nick);
event.message = stripColours(event.message);
channel
.send(`<${event.nick}> ${event.message}`)
.catch(console.error);
}
});
bots[client.user.id].on('nick', function (event) {
event.nick = stripColours(event.nick);
event.new_nick = stripColours(event.new_nick);
Object.values(channels).forEach(channel => {
channel
.send(`**${event.nick}** is now known as **${event.new_nick}**`)
.catch(console.error);
});
});
});
});
client.on('messageCreate', message => {
const ircChannel = getIrcChannelFromDiscordChannel(message.channel.id);
if (!ircChannel || message.author.id == client.user.id)
return;
if (bots[message.author.id] == null) {
client.guilds.fetch(message.guildId).then(guild => {
guild.members.fetch(message.author.id).then(member => {
bots[message.author.id] = createBot(member.nickname, message.author.username, () => {
relayMessage(ircChannel, message);
});
});
});
} else {
relayMessage(ircChannel, message);
}
});
client.on('guildMemberUpdate', (oldMember, newMember) => {
if (bots[newMember.user.id]) {
changeNick(bots[newMember.user.id], newMember.nickname||newMember.user.username)
}
});
function createBot(nick, username, registerCallback) {
console.log(`Creating bot for ${nick||username}`);
var bot = new IRC.Client();
bot.connect({
host: config.host,
port: config.port,
nick: sanitizeDiscordUsername(nick||username),
username: sanitizeDiscordUsername(username).substring(0, 8),
tls: config.port == 6697 ? true : false,
rejectUnauthorized: false,
});
bot.on('registered', () => {
Object.keys(config.channels).forEach(channel => {
bot.join(channel);
});
if (registerCallback != null)
registerCallback();
});
return bot;
}
function relayMessage(ircChannel, message) {
let msg = '';
if(message.mentions && message.mentions.repliedUser)
msg += `${message.mentions.repliedUser.username}: `;
msg += message.content;
bots[message.author.id].say(ircChannel, msg);
if (message.attachments.size > 0) {
message.attachments.forEach(attachment => {
bots[message.author.id].say(ircChannel, attachment.url);
});
}
}
function changeNick(bot, nick){
let newNick = sanitizeDiscordUsername(nick);
bot.changeNick(newNick);
bot.options.nick = newNick;
}
function getIrcChannelFromDiscordChannel(discordChannelId) {
let ircChannel = false;
Object.keys(config.channels).forEach(ircChannelId => {
if (config.channels[ircChannelId] == discordChannelId)
ircChannel = ircChannelId;
});
return ircChannel;
};
function messageIsFromRelay(nick) {
let isFromRelay = false;
Object.values(bots).forEach(bot => {
if (bot.options.nick == nick)
isFromRelay = true;
});
return isFromRelay;
}
function sanitizeDiscordUsername(username) {
return username.replace(/[^a-zA-Z0-9]/g, '').substring(0, config.maxNickLength);
}
function stripColours(str) {
str = str.replace(/\x03[0-9]{1,2}(,[0-9]{1,2})?/g, '');
str = str.replace(/[\x00-\x1F\x7F-\x9F]/g, '');
return str;
}