first commit

This commit is contained in:
longlong5330 2020-08-31 19:10:05 +07:00
commit 03967f0aac
7 changed files with 125 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules

0
README.md Normal file
View file

84
bot.js Normal file
View file

@ -0,0 +1,84 @@
var discord = require('discord.js');
var fs = require('fs');
var randomColour = require('randomcolor'); // yes, the creator of this package does not speak the real english
var Config = require('./config.json');
class Bot {
constructor(){
this.servers = require('./servers.json');
this.discordClient = new discord.Client({sync: true});
this.discordClient.on("ready", () => {
//this.client.user.setActivity('TEN RainBow Role !', { type: 'PLAYING' })
this.initialize();});
this.discordClient.on("message", (msg) => {this.processMessage(msg)});
this.discordClient.login(Config.discord_token);
}
initialize() {
this.log("Connected to discord.");
setInterval(() => {
this.randomizeRoleColors();
}, Config.randomize_delay*1000);
}
processMessage(msg) {
if(msg.content.startsWith(">addrole")) {
for(var role of msg.mentions.roles.array()) {
msg.reply("☑️ Added `" + role.name + "` to list of rainbow roles.");
this.addRainbowRole(msg.guild.id, role.id);
}
}
}
randomizeRoleColors() {
for(var server in this.servers) {
var liveGuild = this.discordClient.guilds.cache.get(server);
if (!liveGuild) {
this.error("Guild with ID " + server+ " no longer exists or the bot has been removed from it.");
continue;
}
for(var role of this.servers[server]) {
var liveRole = liveGuild.roles.cache.get(role);
liveRole.setColor(randomColour(), "Rainbowbot random role color randomizer.");
}
}
}
addRainbowRole(guild, role) {
if(this.servers[guild] == undefined) {
this.servers[guild] = [];
}
for(var existingRole of this.servers[guild]) {
if(existingRole == role) {
return "That role has already been added.";
}
}
this.servers[guild].push(role);
this.saveServers();
}
saveServers() {
fs.writeFileSync("./servers.json", JSON.stringify(this.servers), "utf8");
this.log("Saved servers file.");
}
log(message) {
console.log("\x1b[32mINFO\x1b[37m - \x1b[0m" + message);
}
error(message) {
console.log("\x1b[31mERROR\x1b[37m - \x1b[0m" + message);
}
}
var instance = new Bot();

4
config.json Normal file
View file

@ -0,0 +1,4 @@
{
"discord_token": "NzQ5OTAzMTk3NTIyNjI0NTcy.X0yvxg.s_KBFtW7YVkBJzu6pd02bRtwW7o",
"randomize_delay": 60
}

13
package-lock.json generated Normal file
View file

@ -0,0 +1,13 @@
{
"name": "rainbowbot",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"randomcolor": {
"version": "0.5.4",
"resolved": "https://registry.npmjs.org/randomcolor/-/randomcolor-0.5.4.tgz",
"integrity": "sha512-nYd4nmTuuwMFzHL6W+UWR5fNERGZeVauho8mrJDUSXdNDbao4rbrUwhuLgKC/j8VCS5+34Ria8CsTDuBjrIrQA=="
}
}
}

22
package.json Normal file
View file

@ -0,0 +1,22 @@
{
"name": "rainbowbot",
"version": "1.0.0",
"description": "Randomly changes peoples discord role color.",
"main": "bot.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node bot.js"
},
"keywords": [
"rainbow",
"bot",
"discord",
"role"
],
"author": "Ashleigh Carr",
"license": "MIT",
"dependencies": {
"discord.js": "^11.3.2",
"randomcolor": "^0.5.4"
}
}

1
servers.json Normal file
View file

@ -0,0 +1 @@
{"749899474263867535":["749900156299771985"],"749902372544708649":["749902405696487484"]}