Skip to content

Instantly share code, notes, and snippets.

@Dracovian Dracovian/exabot.js
Created Mar 6, 2019

Embed
What would you like to do?
Exabot's Code
const { Client } = require('discord.js'),
client = new Client(),
mysql = require('mysql'),
process = require('process'),
developer = false;
class Exabot {
constructor(prefix) {
this.prefix = prefix === '' ? prefix : '.';
this.guild = '';
this.months = [
'January', 'February', 'March',
'April', 'May', 'June', 'July',
'August', 'September', 'October',
'November', 'December'
];
this.hostname = '';
this.username = '';
this.password = '';
this.database = '';
this.pool = mysql.createPool({
connectionLimit: 10,
host: this.hostname,
user: this.username,
password: this.password,
database: this.database
});
}
get_member_level(id, callback) {
return this.pool.query('SELECT user_level, user_exp FROM member_level WHERE user_id = ?', [id], async (error, result) => {
if(error && developer)
await console.error(error);
let user_level = result[0]['user_level'],
user_exp = result[0]['user_exp'];
this.pool.query('SELECT level_exp FROM level WHERE level_id = ?', [user_level + 1], async (error2, result2) => {
if(error && developer)
await console.error(error);
callback(user_level, user_exp, result2[0]['level_exp']);
})
});
}
get_member_rank(id, callback) {
return this.pool.query('SELECT user_id FROM member_level ORDER BY user_exp DESC', [], async (error, result) => {
if(error && developer)
await console.error(error);
let j = 1;
for(let i = 0; i < result.length; i++) {
if(result[i]['user_id'] == id)
break;
else j++;
}
await callback(j);
});
}
create_member(id, name) {
this.pool.query('INSERT INTO member (user_id, user_name) VALUES (?, ?)', [id, name], (error) => {
if(error && developer)
console.error(error);
});
this.pool.query('INSERT INTO member_level (user_id, user_exp, user_level) VALUES (?, 0, 1)', [id], (error2) => {
if(error2 && developer)
console.error(error2);
});
}
async update_member_experience(id, exp, username) {
await this.pool.query(`SELECT user_exp, user_level FROM member_level WHERE user_id = ?`, [id], async (error, result) => {
if(error && developer)
await console.error(error);
let current_exp = result[0]['user_exp'],
current_level = result[0]['user_level'];
await this.pool.query('SELECT level_exp from level', [], async (error2, result2) => {
if(error2 && developer)
await console.error(error2);
let level_exp = result2[current_level]['level_exp'],
new_exp = current_exp + exp;
if(new_exp >= level_exp) {
current_level = current_level + 1;
await client.channels.get('').send(`${username} is now level ${current_level}!`);
}
await this.pool.query('UPDATE member_level SET user_exp = ? WHERE user_id = ?', [new_exp, id], async (error3) => {
if(error3 && developer)
await console.error(error3);
});
await this.pool.query('UPDATE member_level SET user_level = ? WHERE user_id = ?', [current_level, id], async (error3) => {
if(error3 && developer)
await console.error(error3);
});
});
});
}
static snowflake_to_timestamp(snowflake) {
try {
let base = BigInt(snowflake).toString(2),
pad = Array((64 - base.length) + 1).join('0'),
bin = pad + base;
return parseInt(bin.substr(0, 42), 2) + 1420070400000;
} catch(error) {
if(developer)
console.error(error);
return 1420070400000;
}
}
timestamp_to_date(timestamp, shorthand = false) {
try {
let date = new Date(timestamp),
month = shorthand ? this.months[date.getMonth()].substr(0, 3) : this.months[date.getMonth()],
minute = date.getMinutes().toLocaleString('en-US', {minimumIntegerDigits: 2, useGrouping: false}),
second = date.getSeconds().toLocaleString('en-US', {minimumIntegerDigits: 2, useGrouping: false});
return `${month} ${date.getDate()}, ${date.getFullYear()} ${date.getHours()}:${minute}:${second}`;
} catch(error) {
if(developer)
console.error(error);
return 'Jan 1, 2014 00:00:01';
}
}
snowflake_to_date(snowflake, shorthand = false) {
let timestamp = Exabot.snowflake_to_timestamp(snowflake);
return this.timestamp_to_date(timestamp, shorthand);
}
static rgb_to_int(red, green, blue) {
try {
let hex_red = red.toString(16).length === 2 ? red.toString(16) : `0${red.toString(16)}`,
hex_green = green.toString(16).length === 2 ? green.toString(16) : `0${green.toString(16)}`,
hex_blue = blue.toString(16).length === 2 ? blue.toString(16) : `0${blue.toString(16)}`,
hex = `${hex_red}${hex_green}${hex_blue}`;
return parseInt(hex, 16);
} catch(error) {
if(developer)
console.error(error);
return parseInt('c00000', 16);
}
}
async ban_member(target, reason) {
try {
await client.channels.get('').send({
embed: {
color: Exabot.rgb_to_int(192, 0, 0),
author: {
name: `${target.username}#${target.discriminator}`,
icon_url: target.avatarURL
},
fields: [{
name: 'Banned',
value: reason,
inline: false
}]
}
});
await client.users.get(target.id).send({
embed: {
color: Exabot.rgb_to_int(192, 0, 0),
author: {
name: client.users.get('').username,
icon_url: client.users.get('').avatarURL
},
fields: [{
name: `You were banned from ${client.guilds.get(this.guild).name}!`,
value: `Reason: ${reason}`,
inline: false
}]
}
});
await client.guilds.get(this.guild).members.get(target.id).ban(7, reason);
} catch(error) {
if(developer)
await console.error(error);
}
}
async kick_member(target, reason) {
await client.channels.get('').send({
embed: {
color: Exabot.rgb_to_int(192, 0, 0),
author: {
name: `${target.username}#${target.discriminator}`,
icon_url: target.avatarURL
},
fields: [{
name: 'Kicked',
value: reason,
inline: false
}]
}
});
await client.users.get(target.id).send({
embed: {
color: Exabot.rgb_to_int(192, 0, 0),
author: {
name: client.users.get('').username,
icon_url: client.users.get('').avatarURL
},
fields: [{
name: `You were kicked from ${client.guilds.get(this.guild).name}!`,
value: `Reason: ${reason}`,
inline: false
}]
}
});
await client.guilds.get(this.guild).members.get(target.id).kick(reason);
}
async warn_member(target, reason) {
await client.channels.get('').send({
embed: {
color: Exabot.rgb_to_int(192, 0, 0),
author: {
name: `${target.username}#${target.discriminator}`,
icon_url: target.avatarURL
},
fields: [{
name: 'Warned',
value: reason,
inline: false
}]
}
});
await client.users.get(target.id).send({
embed: {
color: Exabot.rgb_to_int(192, 0, 0),
author: {
name: client.users.get('').username,
icon_url: client.users.get('').avatarURL
},
fields: [{
name: `You were warned in ${client.guilds.get(this.guild).name}!`,
value: `Reason: ${reason}`,
inline: false
}]
}
})
}
}
const exabot = new Exabot();
client.on('ready', async () => {
await process.stdout.write(`${exabot.timestamp_to_date(new Date().getTime())}: Exabot started.\n`);
});
client.on('guildMemberAdd', async member => {
await client.channels.get('').send({
embed: {
color: Exabot.rgb_to_int(0, 192, 0),
author: {
name: `${member.user.username}#${member.user.discriminator}`,
icon_url: member.user.avatarURL
},
title: 'New Member',
description: exabot.timestamp_to_date(new Date().getTime())
}
});
exabot.create_member(member.id, member.username);
});
client.on('message', async message => {
if(!message.author.bot && message.content.startsWith(exabot.prefix)) {
let command = message.content.replace(exabot.prefix, '');
if(message.member.roles.some(role => ['Staff'].includes(role.name))) {
if(command.startsWith('ban')) {
let splices = command.split(' '),
target = message.mentions.users.first(),
reason = splices.length >= 3 ? splices.slice(2, splices.length).join(' ') : 'No reason given.';
await message.delete();
await exabot.ban_member(target, reason);
}
if(command.startsWith('kick')) {
let splices = command.split(' '),
target = message.mentions.users.first(),
reason = splices.length >= 3 ? splices.slice(2, splices.length).join(' ') : 'No reason given.';
await message.delete();
await exabot.kick_member(target, reason);
}
if(command.startsWith('warn')) {
let splices = command.split(' '),
target = message.mentions.users.first(),
reason = splices.length >= 3 ? splices.slice(2, splices.length).join(' ') : 'No reason given.';
await message.delete();
exabot.warn_member(target, reason);
}
if(command.startsWith('create')) {
let target = message.mentions.users.first();
await message.delete();
exabot.create_member(target.id, target.username);
}
}
if(command.startsWith('userinfo')) {
let target = message.mentions.users.first(),
creation = exabot.snowflake_to_date(target.id);
await message.delete();
await message.channel.send({
embed: {
color: Exabot.rgb_to_int(0, 192, 0),
author: {
name: `${target.username}#${target.discriminator}`,
icon_url: target.avatarURL
},
fields: [
{
name: '‍‍',
value: 'ID\nCreation',
inline: true
},
{
name: '‍‍',
value: `${target.id}\n${creation}`,
inline: true
}
]
}
});
}
if(command === 'serverinfo') {
let creation = exabot.snowflake_to_date(message.guild.id);
await message.delete();
await message.channel.send({
embed: {
color: Exabot.rgb_to_int(0, 192, 0),
author: {
name: message.guild.name,
icon_url: message.guild.iconURL
},
fields: [
{
name: '‍',
value: 'Name\nOwner\nCreation',
inline: true
},
{
name: '‍',
value: `${message.guild.name}\n${message.guild.owner}\n${creation}`,
inline: true
}
]
}
});
}
if(command.startsWith('rank')) {
let target = message.mentions.users.first();
exabot.get_member_rank(target.id, async rank => {
await message.delete();
await message.channel.send({
embed: {
color: Exabot.rgb_to_int(0, 0, 192),
author: {
name: `${target.username}#${target.discriminator}`,
icon_url: target.avatarURL
},
fields: [{
name: `Rank`,
value: `${rank} of ${message.guild.memberCount}`,
inline: false
}]
}
});
});
}
if(command.startsWith('level')) {
let target = message.mentions.users.first();
exabot.get_member_level(target.id, async (level, exp, max) => {
await message.delete();
await message.channel.send({
embed: {
color: Exabot.rgb_to_int(0, 0, 192),
author: {
name: `${target.username}#${target.discriminator}`,
icon_url: target.avatarURL
},
fields: [
{
name: '‌',
value: 'Level\nExperience',
inline: true
},
{
name: '‌',
value: `${level} of 500\n${exp} of ${max}`,
inline: true
}
]
}
});
});
}
}
if(!message.author.bot && message.channel.id === '') {
let postExp = Math.round(((Math.random() * 1000) % 25)) + 10,
id = BigInt(message.author.id);
await exabot.update_member_experience(id, postExp, message.author.username);
}
});
client.on('warn', async info => {
if(developer) await console.log(info);
else await process.stdout.write('');
});
client.on('error', async error => {
if(developer) await console.error(error);
else await process.stdout.write('');
});
client.on('debug', async info => {
if(developer) await console.debug(info);
else await process.stdout.write('');
});
client.login('');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.