mirror of
https://github.com/pacnpal/markov-discord.git
synced 2025-12-20 03:01:04 -05:00
Update config loading logic. Refactor isModerator
This commit is contained in:
36
index.ts
36
index.ts
@@ -22,6 +22,12 @@ interface MessagesDB {
|
||||
messages: MessageRecord[];
|
||||
}
|
||||
|
||||
interface MarkbotConfig {
|
||||
prefix: string;
|
||||
game: string;
|
||||
token?: string;
|
||||
}
|
||||
|
||||
const version: string = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version || '0.0.0';
|
||||
|
||||
const client = new Discord.Client();
|
||||
@@ -52,6 +58,7 @@ const markovOpts = {
|
||||
minScorePerWord: 0,
|
||||
maxTries: 10000,
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
function uniqueBy<Record extends { [key: string]: any }>(
|
||||
arr: Record[],
|
||||
@@ -126,28 +133,29 @@ function loadConfig(): void {
|
||||
fs.renameSync('./markovDB.json', './config/markovDB.json');
|
||||
}
|
||||
|
||||
let token = 'missing';
|
||||
try {
|
||||
const cfg = JSON.parse(fs.readFileSync('./config/config.json', 'utf8'));
|
||||
PREFIX = cfg.prefix;
|
||||
GAME = cfg.game;
|
||||
client.login(cfg.token);
|
||||
const cfg: MarkbotConfig = JSON.parse(fs.readFileSync('./config/config.json', 'utf8'));
|
||||
PREFIX = cfg.prefix || '!mark';
|
||||
GAME = cfg.game || '!mark help';
|
||||
token = cfg.token || process.env.TOKEN || token;
|
||||
} catch (e) {
|
||||
console.warn(
|
||||
'Failed to use config.json. using default configuration with token environment variable'
|
||||
);
|
||||
PREFIX = '!mark';
|
||||
GAME = '"!mark help" for help';
|
||||
client.login(process.env.TOKEN);
|
||||
console.error('Failed to read config.json.');
|
||||
throw e;
|
||||
}
|
||||
try {
|
||||
client.login(token);
|
||||
} catch (e) {
|
||||
console.error('Failed to login with token:', token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the author of a message as moderator-like permissions.
|
||||
* @param {Message} message Message object to get the sender of the message.
|
||||
* @param {GuildMember} member Sender of the message
|
||||
* @return {Boolean} True if the sender is a moderator.
|
||||
*/
|
||||
function isModerator(message: Discord.Message): boolean {
|
||||
const { member } = message;
|
||||
function isModerator(member: Discord.GuildMember): boolean {
|
||||
return (
|
||||
member.hasPermission('ADMINISTRATOR') ||
|
||||
member.hasPermission('MANAGE_CHANNELS') ||
|
||||
@@ -346,7 +354,7 @@ client.on('message', message => {
|
||||
});
|
||||
}
|
||||
if (command === 'train') {
|
||||
if (isModerator(message)) {
|
||||
if (isModerator(message.member)) {
|
||||
console.log('Training...');
|
||||
fileObj = {
|
||||
messages: [],
|
||||
|
||||
Reference in New Issue
Block a user