70 lines
2.4 KiB
JavaScript
70 lines
2.4 KiB
JavaScript
|
require('dotenv').config();
|
||
|
const Revolt = require('revolt.js');
|
||
|
const { default: Logger, LogLevel } = require('log75');
|
||
|
|
||
|
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',
|
||
|
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);
|
||
|
const channel = await client.channels.fetch(message.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;
|
||
|
|
||
|
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
|
||
|
});
|
||
|
|
||
|
module.exports.client = client;
|
||
|
module.exports.logger = logger;
|
||
|
module.exports.config = config;
|
||
|
|
||
|
const { commands } = require('./util/command_loader');
|