diff --git a/src/commands/ban.ts b/src/commands/ban.ts index 68d7b33..cd0d74a 100644 --- a/src/commands/ban.ts +++ b/src/commands/ban.ts @@ -6,4 +6,4 @@ export function command (message: discord.Message) { message.mentions.users.map(async (user) => { await ban(user, message.author, message.guild); }); -}; +} diff --git a/src/commands/clearWarnings.ts b/src/commands/clearWarnings.ts index a95e727..b6e9f51 100644 --- a/src/commands/clearWarnings.ts +++ b/src/commands/clearWarnings.ts @@ -18,4 +18,4 @@ export function command (message: discord.Message) { logger.info(`${message.author.username} has cleared all warnings for ${user} ${user.username} [${count?.length}].`); await state.logChannel?.send(`${message.author.toString()} has cleared all warnings for ${user.toString()} [${count?.length}].`); }); -}; +} diff --git a/src/commands/game.ts b/src/commands/game.ts index 4678565..995bc83 100644 --- a/src/commands/game.ts +++ b/src/commands/game.ts @@ -29,7 +29,7 @@ async function updateDatabase () { } try { - let response = await fetch(targetServer); + const response = await fetch(targetServer); body = await response.json(); } catch (e) { logger.error('Unable to download latest games list!'); @@ -104,5 +104,5 @@ export async function command (message: discord.Message) { .setURL(url) .setThumbnail(screenshot); - await message.channel.send({embeds: [embed]}); + await message.channel.send({ embeds: [embed] }); } diff --git a/src/commands/grantDeveloper.ts b/src/commands/grantDeveloper.ts index 27214de..a4dc24f 100644 --- a/src/commands/grantDeveloper.ts +++ b/src/commands/grantDeveloper.ts @@ -30,9 +30,8 @@ export function command (message: discord.Message) { logger.error(`Error granting ${user} ${user.username}'s developer speech...`); }); } - }).catch(async () => { + }).catch(async () => { await message.channel.send(`User ${user.toString()} was not found in the channel.`); }); - }); } diff --git a/src/commands/info.ts b/src/commands/info.ts index d5e90d7..a7fa348 100644 --- a/src/commands/info.ts +++ b/src/commands/info.ts @@ -24,4 +24,4 @@ export async function command (message: discord.Message) { await message.channel.send(`\`${user.username} (${totalWarnings}) information:\`${warns.length !== 0 ? warnsString : '\n\n'}${bans.length !== 0 ? bansString : ''}`); }); -}; +} diff --git a/src/commands/status.ts b/src/commands/status.ts index 6193162..9875421 100644 --- a/src/commands/status.ts +++ b/src/commands/status.ts @@ -2,12 +2,12 @@ import fetch from 'node-fetch'; import discord = require('discord.js'); const fetchOptions = { - headers: { 'User-Agent': 'Citra-Emu/CitraBot (Node.js)', 'Accept': 'application/vnd.github.antiope-preview+json' } + headers: { 'User-Agent': 'Citra-Emu/CitraBot (Node.js)', Accept: 'application/vnd.github.antiope-preview+json' } }; const repo = process.env.GITHUB_REPOSITORY || 'citra-emu/citra'; export const roles = ['Admins', 'Moderators', 'Developer']; -export function command(message: discord.Message) { +export function command (message: discord.Message) { const pr_number = message.content.substr(message.content.indexOf(' ') + 1).replace(/\n/g, ''); const url = `https://api.github.com/repos/${repo}/pulls/${pr_number}`; fetch(url, fetchOptions).then(response => response.json()).then((pr: any) => { @@ -16,7 +16,7 @@ export function command(message: discord.Message) { // use the new GitHub checks API fetch(`https://api.github.com/repos/${repo}/commits/${headSHA}/check-runs`, fetchOptions).then(response => response.json()).then(async (statuses: any) => { if (!statuses.check_runs || statuses.total_count < 1) throw new Error('No check runs'); - let msg = new discord.MessageEmbed().setTitle(`Status for PR #${pr_number}`).setURL(pr.html_url); + const msg = new discord.MessageEmbed().setTitle(`Status for PR #${pr_number}`).setURL(pr.html_url); let color = 'GREEN' as discord.ColorResolvable; statuses.check_runs.forEach((run: any) => { msg.addField(`${run.name}`, `**[${run.status} ${run.conclusion}](${run.html_url})**`); @@ -25,7 +25,7 @@ export function command(message: discord.Message) { msg.setColor(color); await message.channel.send({ embeds: [msg] }); }).catch(async () => { - await message.channel.send('I wasn\'t able to get the status of that PR...') + await message.channel.send('I wasn\'t able to get the status of that PR...'); }); }).catch(async () => { await message.channel.send('No such PR.'); diff --git a/src/common.ts b/src/common.ts index bfc15a2..8117a51 100644 --- a/src/common.ts +++ b/src/common.ts @@ -4,17 +4,17 @@ import logger from './logging'; import UserBan from './models/UserBan'; import discord = require('discord.js'); -export async function ban(user: discord.User, moderator: discord.User, guild: discord.Guild | null) { - const count = state.warnings.filter(x => x.id === user.id && !x.cleared).length || 0; +export async function ban (user: discord.User, moderator: discord.User, guild: discord.Guild | null) { + const count = state.warnings.filter(x => x.id === user.id && !x.cleared).length || 0; - logger.info(`${moderator.toString()} has banned ${user.toString()} ${user.id} ${user.username}.`); - await state.logChannel?.send(`${moderator.toString()} has banned ${user.id} ${user.toString()} [${count}].`); + logger.info(`${moderator.toString()} has banned ${user.toString()} ${user.id} ${user.username}.`); + await state.logChannel?.send(`${moderator.toString()} has banned ${user.id} ${user.toString()} [${count}].`); - state.bans.push(new UserBan(user.id, user.username, moderator.id, moderator.username, count)); - guild?.members?.ban(user).catch(async function (error) { - await state.logChannel?.send(`Error banning ${user.toString()} ${user.username}`); - logger.error(`Error banning ${user.toString()} ${user.id} ${user.username}.`, error); - }); + state.bans.push(new UserBan(user.id, user.username, moderator.id, moderator.username, count)); + guild?.members?.ban(user).catch(async function (error) { + await state.logChannel?.send(`Error banning ${user.toString()} ${user.username}`); + logger.error(`Error banning ${user.toString()} ${user.id} ${user.username}.`, error); + }); - data.flushBans(); + data.flushBans(); } diff --git a/src/logging.ts b/src/logging.ts index 1995733..d6a0940 100644 --- a/src/logging.ts +++ b/src/logging.ts @@ -13,7 +13,7 @@ const logger = winston.createLogger({ handleExceptions: true }) ], - exitOnError: true, + exitOnError: true }); // Setup logging for LogDNA cloud logging. diff --git a/src/server.ts b/src/server.ts index d42380a..6a7bbc2 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,6 +1,4 @@ // Check for environmental variables. -require('checkenv').check(); - import discord = require('discord.js'); import path = require('path'); import fs = require('fs'); @@ -10,6 +8,8 @@ import state from './state'; import * as data from './data'; import { IModule, ITrigger } from './models/interfaces'; +require('checkenv').check(); + interface IModuleMap { [name: string]: IModule; } @@ -29,13 +29,13 @@ if (!rluesRole) { throw new Error('DISCORD_RULES_ROLE somehow became undefined.'); } -function findArray(haystack: string | any[], arr: any[]) { +function findArray (haystack: string | any[], arr: any[]) { return arr.some(function (v: any) { return haystack.indexOf(v) >= 0; }); } -function IsIgnoredCategory(categoryName: string) { +function IsIgnoredCategory (categoryName: string) { const IgnoredCategory = ['internal', 'team', 'development']; return IgnoredCategory.includes(categoryName); } @@ -45,8 +45,8 @@ client.on('ready', async () => { if (!process.env.DISCORD_LOG_CHANNEL || !process.env.DISCORD_MSGLOG_CHANNEL) { throw new Error('DISCORD_LOG_CHANNEL or DISCORD_MSGLOG_CHANNEL not defined.'); } - let logChannel = await client.channels.fetch(process.env.DISCORD_LOG_CHANNEL) as discord.TextChannel; - let msglogChannel = await client.channels.fetch(process.env.DISCORD_MSGLOG_CHANNEL) as discord.TextChannel; + const logChannel = await client.channels.fetch(process.env.DISCORD_LOG_CHANNEL) as discord.TextChannel; + const msglogChannel = await client.channels.fetch(process.env.DISCORD_MSGLOG_CHANNEL) as discord.TextChannel; if (!logChannel.send) throw new Error('DISCORD_LOG_CHANNEL is not a text channel!'); if (!msglogChannel.send) throw new Error('DISCORD_MSGLOG_CHANNEL is not a text channel!'); state.logChannel = logChannel; @@ -69,22 +69,21 @@ client.on('disconnect', () => { }); client.on('guildMemberAdd', async (member) => { - if (process.env.DISCORD_RULES_ROLE) - await member.roles.add(process.env.DISCORD_RULES_ROLE); + if (process.env.DISCORD_RULES_ROLE) { await member.roles.add(process.env.DISCORD_RULES_ROLE); } }); client.on('messageDelete', async (message) => { const AllowedRoles = ['Administrators', 'Moderators', 'Team', 'Developer', 'Support', 'VIP']; - let authorRoles = message.member?.roles?.cache?.map(x => x.name); + const authorRoles = message.member?.roles?.cache?.map(x => x.name); if (!authorRoles) { logger.error(`Unable to get the roles for ${message.author}`); return; } if (!findArray(authorRoles, AllowedRoles)) { - let parent = (message.channel as discord.TextChannel).parent; + const parent = (message.channel as discord.TextChannel).parent; if (parent && IsIgnoredCategory(parent.name) === false) { if (((message.content && message.content.startsWith('.') === false) || (message.attachments.size > 0)) && message.author?.bot === false) { - let messageAttachment = message.attachments.first()?.proxyURL + const messageAttachment = message.attachments.first()?.proxyURL; const deletionEmbed = new discord.MessageEmbed() .setAuthor(message.author?.tag, message.author?.displayAvatarURL()) @@ -93,9 +92,9 @@ client.on('messageDelete', async (message) => { .setTimestamp() .setColor('RED'); - if (messageAttachment) deletionEmbed.setImage(messageAttachment) + if (messageAttachment) deletionEmbed.setImage(messageAttachment); - let userInfo = `${message.author?.toString()} (${message.author?.username}) (${message.author})` + const userInfo = `${message.author?.toString()} (${message.author?.username}) (${message.author})`; await state.msglogChannel?.send({ content: userInfo, embeds: [deletionEmbed] }); logger.info(`${message.author?.username} ${message.author} deleted message: ${message.cleanContent}.`); @@ -106,18 +105,18 @@ client.on('messageDelete', async (message) => { client.on('messageUpdate', async (oldMessage, newMessage) => { const AllowedRoles = ['Administrators', 'Moderators', 'Team', 'Developer', 'Support', 'VIP']; - let authorRoles = oldMessage.member?.roles?.cache?.map(x => x.name); + const authorRoles = oldMessage.member?.roles?.cache?.map(x => x.name); if (!authorRoles) { logger.error(`Unable to get the roles for ${oldMessage.author}`); return; } if (!findArray(authorRoles, AllowedRoles)) { - let parent = (oldMessage.channel as discord.TextChannel).parent; + const parent = (oldMessage.channel as discord.TextChannel).parent; if (parent && IsIgnoredCategory(parent.name) === false) { const oldM = oldMessage.cleanContent || ''; const newM = newMessage.cleanContent; if (oldMessage.content !== newMessage.content && oldM && newM) { - let messageAttachment = oldMessage.attachments.first()?.proxyURL + const messageAttachment = oldMessage.attachments.first()?.proxyURL; const editedEmbed = new discord.MessageEmbed() .setAuthor(oldMessage.author?.tag || '', oldMessage.author?.displayAvatarURL()) @@ -127,9 +126,9 @@ client.on('messageUpdate', async (oldMessage, newMessage) => { .setTimestamp() .setColor('GREEN'); - if (messageAttachment) editedEmbed.setImage(messageAttachment) + if (messageAttachment) editedEmbed.setImage(messageAttachment); - let userInfo = `${oldMessage.author?.toString()} (${oldMessage.author?.username}) (${oldMessage.author})` + const userInfo = `${oldMessage.author?.toString()} (${oldMessage.author?.username}) (${oldMessage.author})`; await state.msglogChannel?.send({ content: userInfo, embeds: [editedEmbed] }); logger.info(`${oldMessage.author?.username} ${oldMessage.author} edited message from: ${oldM} to: ${newM}.`); @@ -151,7 +150,7 @@ client.on('messageCreate', async (message) => { logger.verbose(`${message.author.username} ${message.author} [Channel: ${(message.channel as discord.TextChannel).name} ${message.channel}]: ${message.content}`); - let authorRoles = message.member?.roles?.cache?.map(x => x.name); + const authorRoles = message.member?.roles?.cache?.map(x => x.name); if (message.channel.id === process.env.DISCORD_MEDIA_CHANNEL && !message.author.bot) { const AllowedMediaRoles = ['Administrators', 'Moderators', 'Team', 'VIP']; @@ -187,7 +186,7 @@ client.on('messageCreate', async (message) => { const cmd = message.content.split(' ', 1)[0].slice(1); // Check by the name of the command. - let cachedModule = cachedModules[`${cmd.toLowerCase()}`]; + const cachedModule = cachedModules[`${cmd.toLowerCase()}`]; let quoteResponse = null; // Check by the quotes in the configuration. if (!cachedModule) quoteResponse = state.responses.quotes[cmd]; @@ -208,13 +207,12 @@ client.on('messageCreate', async (message) => { await message.delete(); try { - if (!!cachedModule) { + if (cachedModule) { await cachedModule.command(message); - } else if (cachedModules['quote']) { - await cachedModules['quote'].command(message, quoteResponse?.reply); + } else if (cachedModules.quote) { + await cachedModules.quote.command(message, quoteResponse?.reply); } } catch (err) { logger.error(err); } - } else if (message.author.bot === false) { // This is a normal channel message. cachedTriggers.forEach(async function (trigger) { diff --git a/src/triggers/pingBomb.ts b/src/triggers/pingBomb.ts index 9684051..e1b1f0c 100644 --- a/src/triggers/pingBomb.ts +++ b/src/triggers/pingBomb.ts @@ -3,13 +3,13 @@ import state from '../state'; import logger from '../logging'; import discord = require('discord.js'); -export function trigger(message: discord.Message) { - return message.mentions.users.size > 10; +export function trigger (message: discord.Message) { + return message.mentions.users.size > 10; } -export async function execute(message: discord.Message) { - const count = message.mentions.users.size; - logger.info(`${message.author.toString()} tagged ${count} users in ${message.channel.toString()}`); - state.logChannel?.send(`Ping bomb detected in ${message.channel.toString()} by ${message.author.toString()}`); - await ban(message.author, message.author, message.guild); -}; +export async function execute (message: discord.Message) { + const count = message.mentions.users.size; + logger.info(`${message.author.toString()} tagged ${count} users in ${message.channel.toString()}`); + state.logChannel?.send(`Ping bomb detected in ${message.channel.toString()} by ${message.author.toString()}`); + await ban(message.author, message.author, message.guild); +}