revolt-bot/index.js

93 lines
3.4 KiB
JavaScript

require('dotenv').config();
const Revolt = require('revolt.js');
const Enmap = require('enmap');
const { default: Logger, LogLevel } = require('log75');
const db = {
blacklist: new Enmap({ name: 'userBlacklist' }),
devs: new Enmap({ name: 'devList' })
}
const config = {
revoltClient: {
apiURL: process.env.API_URL || 'https://api.revolt.chat',
autoReconnect: true,
debug: false
},
prefix: process.env.BOT_PREFIX || '/',
debug: process.env.NODE_ENV !== 'production',
botOwner: process.env.BOT_OWNER || null,
creds: {
// chrome debug -> Application -> IndexedDB -> localforage -> state -> auth.accounts[user ID].session
session_token: process.env.SESSION_TOKEN,
user_id: process.env.USER_ID,
id: process.env.SESSION_ID
}
}
const logger = new Logger(config.debug ? LogLevel.Debug : LogLevel.Standard);
const client = new Revolt.Client(config.revoltClient);
client.on("message", async message => {
const author = await client.users.fetch(message.author).catch(()=>{});
const channel = await client.channels.fetch(message.channel).catch(()=>{});
if (!author || !channel) return logger.debug(`Received message with unknown author or channel`);
if (author._id === client.user._id)
return logger.debug(`Sent message -> ${channel.name} => ${message.content}`);
const logPrefix = `${author.username} (${author._id}) in ${channel.name || 'Unnamed'} (${channel._id})`;
logger.debug(`${logPrefix} => ${message.content}`);
if (!message.content.startsWith(config.prefix)) return;
let args = message.content.substr(config.prefix.length).split(' ');
let commandName = args.shift();
let command = commands.find(cmd => cmd.meta.name === commandName || cmd.meta.aliases?.indexOf(commandName) > -1);
if (!command) return;
let userDevLvl = require('./util/dev').getDevLevel(message.author);
let blacklistStatus = db.blacklist.get(message.author);
if (blacklistStatus?.blacklisted && userDevLvl <= 0) {
logger.warn(`Rejected blocked user`);
if (!blacklistStatus.silent && blacklistStatus.nextWarning < Date.now()) {
await client.channels.sendMessage(message.channel, `<@${message.author}>, you're currently not allowed to use this bot.`);
}
blacklistStatus.nextWarning = Date.now() + 1000 * 60 * 2;
db.blacklist.set(message.author, blacklistStatus);
return;
}
if (userDevLvl < (command.meta.devLevel ?? 0)) return logger.warn(`Rejected dev command`);
logger.info(`${logPrefix} -> COMMAND ${config.prefix}${commandName} ${args?.join(' ')}`)
try {
command.run(message, args);
} catch(e) {
await client.channels.sendMessage(message.channel, `bot is death\n\`\`\`js\n${e}\`\`\``);
}
});
// Log in
if (!config.creds.session_token) return logger.error('gimme token or die');
if (!config.creds.user_id) return logger.error('gimme user id or die');
if (!config.creds.id) return logger.error('gimme session id or die');
logger.info(`Attempting to continue session...`);
client.useExistingSession(config.creds)
.then(() => {
logger.done('Logged in');
logger.info(`${client.user.username} (${client.user._id}): Ready`);
require('./util/friendships'); // Manage relationships
require('./util/levels'); // Level ups
});
module.exports = { client, logger, config, db }
const { commands } = require('./util/command_loader');