Initial commit

master
KaKi87 4 years ago
parent 2a4e062178
commit 0ccf546193
  1. 4
      .gitignore
  2. 32
      config.example.json
  3. 13
      index.js
  4. 61
      messageHandler.js
  5. 14
      package.json
  6. 52
      yarn.lock

4
.gitignore vendored

@ -58,4 +58,8 @@ typings/
# dotenv environment variables file
.env
# JetBrains
.idea
# Bot config
config.json

@ -0,0 +1,32 @@
{
"token": "",
"guild": "",
"messages": [
{
"channel": "channel",
"title": "",
"body": ""
},
{
"channels": [
"channel",
"channel"
],
"title": "",
"body": ""
},
{
"channel": "category",
"title": "",
"body": ""
},
{
"channels": [
"category",
"category"
],
"title": "",
"body": ""
}
]
}

@ -0,0 +1,13 @@
const Discord = require('discord.js');
const config = require('./config.json');
const messageHandler = require('./messageHandler.js');
const Client = new Discord.Client();
Client.login(config.token);
Client.on('ready', () => messageHandler.init(Client.user.id, Client.guilds.get(config.guild), config.messages));
Client.on('message', message => messageHandler.handle(message));

@ -0,0 +1,61 @@
const handler = {};
let ClientID = null,
Guild = null;
let addTextChannel = (textChannel, title, body) => {
handler[textChannel] = { title, body };
};
let add = (channel, title, body) => {
switch(Guild.channels.get(channel).type){
case 'category':
Guild.channels.filter(c => c.parentID === channel).forEach(textChannel => {
addTextChannel(textChannel.id, title, body);
});
break;
case 'text':
addTextChannel(channel, title, body);
break;
}
};
let sendMessage = channel => {
Guild.channels.get(channel).send({
embed: {
title: handler[channel]['title'],
fields: [
{
name: "** **",
value: handler[channel]['body']
}
]
}
});
};
let deleteLastMessage = (channel, callback) => {
channel.fetchMessages({ limit: 100 }).then(messages => {
let lastMessage = messages.find(message => message.author.id === ClientID);
if(!lastMessage) return callback();
lastMessage.delete().then(() => callback());
});
};
module.exports = {
init: (clientId, guild, messages) => {
ClientID = clientId;
Guild = guild;
messages.forEach(item => {
if(item['channels']) return item['channels'].forEach(channel => {
add(channel, item['title'], item['body']);
});
add(item['channel'], item['title'], item['body']);
});
Object.keys(handler).forEach(channel => deleteLastMessage(Guild.channels.get(channel), () => sendMessage(channel)));
},
handle: message => {
if(!handler[message.channel.id] || message.channel.lastMessage.author.id === ClientID) return;
deleteLastMessage(message.channel, () => sendMessage(message.channel.id));
}
};

@ -0,0 +1,14 @@
{
"name": "repeater-bot",
"version": "1.0.0",
"main": "index.js",
"repository": "https://git.kaki87.net/KaKi87/repeater-bot",
"author": "KaKi87 <tiana.lemesle@live.fr>",
"license": "GPL-2.0-only",
"dependencies": {
"discord.js": "^11.4.2"
},
"scripts": {
"start": "node index.js"
}
}

@ -0,0 +1,52 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
async-limiter@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8"
integrity sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==
discord.js@^11.4.2:
version "11.4.2"
resolved "https://registry.yarnpkg.com/discord.js/-/discord.js-11.4.2.tgz#54586981926521572051f2a30b984aad2b49786e"
integrity sha512-MDwpu0lMFTjqomijDl1Ed9miMQe6kB4ifKdP28QZllmLv/HVOJXhatRgjS8urp/wBlOfx+qAYSXcdI5cKGYsfg==
dependencies:
long "^4.0.0"
prism-media "^0.0.3"
snekfetch "^3.6.4"
tweetnacl "^1.0.0"
ws "^4.0.0"
long@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/long/-/long-4.0.0.tgz#9a7b71cfb7d361a194ea555241c92f7468d5bf28"
integrity sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==
prism-media@^0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/prism-media/-/prism-media-0.0.3.tgz#8842d4fae804f099d3b48a9a38e3c2bab6f4855b"
integrity sha512-c9KkNifSMU/iXT8FFTaBwBMr+rdVcN+H/uNv1o+CuFeTThNZNTOrQ+RgXA1yL/DeLk098duAeRPP3QNPNbhxYQ==
safe-buffer@~5.1.0:
version "5.1.2"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==
snekfetch@^3.6.4:
version "3.6.4"
resolved "https://registry.yarnpkg.com/snekfetch/-/snekfetch-3.6.4.tgz#d13e80a616d892f3d38daae4289f4d258a645120"
integrity sha512-NjxjITIj04Ffqid5lqr7XdgwM7X61c/Dns073Ly170bPQHLm6jkmelye/eglS++1nfTWktpP6Y2bFXjdPlQqdw==
tweetnacl@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-1.0.1.tgz#2594d42da73cd036bd0d2a54683dd35a6b55ca17"
integrity sha512-kcoMoKTPYnoeS50tzoqjPY3Uv9axeuuFAZY9M/9zFnhoVvRfxz9K29IMPD7jGmt2c8SW7i3gT9WqDl2+nV7p4A==
ws@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-4.1.0.tgz#a979b5d7d4da68bf54efe0408967c324869a7289"
integrity sha512-ZGh/8kF9rrRNffkLFV4AzhvooEclrOH0xaugmqGsIfFgOE/pIz4fMc4Ef+5HSQqTEug2S9JZIWDR47duDSLfaA==
dependencies:
async-limiter "~1.0.0"
safe-buffer "~5.1.0"