Skip to content

Commit

Permalink
Merge pull request #277 from aternosorg/log
Browse files Browse the repository at this point in the history
Split message and moderation logs
  • Loading branch information
JulianVennen authored Jun 6, 2021
2 parents 8fdafa3 + f142258 commit 0681be7
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 66 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "modbot",
"version": "0.3.1",
"version": "0.4.0",
"description": "Discord Bot for the Aternos Discord server",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/GuildConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class GuildConfig extends Config {
* @param {module:"discord.js".Snowflake} id guild id
* @param {Object} [json] options
* @param {module:"discord.js".Snowflake} [json.logChannel] id of the log channel
* @param {module:"discord.js".Snowflake} [json.messageLogChannel] if of the message log channel
* @param {module:"discord.js".Snowflake} [json.mutedRole] id of the muted role
* @param {module:"discord.js".Snowflake[]} [json.modRoles] role ids that can execute commands
* @param {module:"discord.js".Snowflake[]} [json.protectedRoles] role ids that can't be targeted by moderations
Expand All @@ -40,6 +41,7 @@ class GuildConfig extends Config {
super(id);

this.logChannel = json.logChannel;
this.messageLogChannel = json.messageLogChannel;
this.mutedRole = json.mutedRole;
if (json.modRoles instanceof Array)
this.#modRoles = json.modRoles;
Expand Down
63 changes: 52 additions & 11 deletions src/Log.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,11 @@ class Log{
/** @type {module:"discord.js".Guild} */
const guild = await util.resolveGuild(guildInfo);

/** @type {GuildConfig} */
const guildConfig = await GuildConfig.get(/** @type {module:"discord.js".Snowflake} */guild.id);
if (!guildConfig.logChannel) return null;

try {
return await guild.channels.resolve(/** @type {String} */guildConfig.logChannel).send(message.substring(0,2000),options);
}
catch (e) {
if ([APIErrors.MISSING_ACCESS, APIErrors.MISSING_PERMISSIONS].includes(e.code)) {
return null;
}
else {
throw e;
}
}
return this._send(guild.channels.resolve(/** @type {String} */guildConfig.logChannel), message, options)
}

/**
Expand Down Expand Up @@ -72,6 +63,34 @@ class Log{
}));
};

/**
* Logs a message to the guilds message log channel (if specified)
* @param {GuildInfo} guildInfo
* @param {String} message content of the log message
* @param {module:"discord.js".MessageEmbed} [options]
* @return {Promise<module:"discord.js".Message|null>} log message
*/
static async messageLog(guildInfo, message, options) {
/** @type {module:"discord.js".Guild} */
const guild = await util.resolveGuild(guildInfo);

/** @type {GuildConfig} */
const guildConfig = await GuildConfig.get(/** @type {module:"discord.js".Snowflake} */guild.id);
if (!guildConfig.messageLogChannel) return null;

return this._send(guild.channels.resolve(/** @type {String} */guildConfig.messageLogChannel), message, options)
}

/**
* Logs an embed to the guilds message log channel (if specified)
* @param {GuildInfo} guildInfo
* @param {module:"discord.js".MessageEmbed|Object} embed embed to log
* @return {Promise<module:"discord.js".Message|null>} log message
*/
static async messageLogEmbed(guildInfo, embed) {
return this.messageLog(guildInfo,'',new Discord.MessageEmbed(embed));
}

/**
* Log a moderation
* @async
Expand Down Expand Up @@ -132,6 +151,28 @@ class Log{

return this.logEmbed(guildInfo,logEmbed);
};

/**
* try to send this message to this channel
* @param {module:"discord.js".GuildChannel} channel
* @param {String} message
* @param {module:"discord.js".MessageEmbed} [options]
* @returns {Promise<null|*>}
* @private
*/
static async _send(channel, message, options) {
try {
return channel.send(message.substring(0,2000),options);
}
catch (e) {
if ([APIErrors.MISSING_ACCESS, APIErrors.MISSING_PERMISSIONS].includes(e.code)) {
return null;
}
else {
throw e;
}
}
}
}

module.exports = Log;
50 changes: 0 additions & 50 deletions src/commands/legacy/logchannel.js

This file was deleted.

57 changes: 57 additions & 0 deletions src/commands/settings/LogChannelCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const Command = require('../../Command');
const util = require('../../util.js');
const Discord = require('discord.js');

class LogChannelCommand extends Command {

static description = 'Configure the channel that moderations will be logged in';

static usage = '<#channel|id>|off|status';

static names = ['log','logchannel'];

static userPerms = ['MANAGE_GUILD'];

async execute() {
if (this.args.length !== 1) {
await this.sendUsage();
return;
}

switch (this.args[0].toLowerCase()) {
case "off":
this.guildConfig.logChannel = null;
await this.guildConfig.save();
await this.message.channel.send(new Discord.MessageEmbed()
.setDescription("Disabled moderation logs")
.setFooter("You can configure message logs with the 'messagelogs' command.")
.setColor(util.color.red)
);
break;
case "status":
await this.message.channel.send(new Discord.MessageEmbed()
.setDescription(`Moderations are currently ${this.guildConfig.logChannel ? `logged to <#${this.guildConfig.logChannel}>` :
`not logged.\n Use \`${this.prefix}log ${LogChannelCommand.usage}\` to change this`}.`)
.setFooter(`You can configure message logs with ${this.prefix}messagelogs`)
.setColor(this.guildConfig.logChannel ? util.color.green : util.color.red)
);
break;
default:
const channel = util.channelMentionToId(this.args[0]);
if (channel === null || !await util.isChannel(this.message.guild, channel)) return this.sendUsage();
if (!this.message.guild.channels.resolve(/** @type {Snowflake} */channel).permissionsFor(this.bot.user).has(['SEND_MESSAGES', 'VIEW_CHANNEL'])){
return this.message.channel.send('I am missing the required permissions to send messages to that channel!');
}

this.guildConfig.logChannel = channel;
await this.guildConfig.save();
await this.message.channel.send(new Discord.MessageEmbed()
.setDescription(`Set moderation log channel to <#${channel}>.`)
.setFooter("You can configure message logs with the 'messagelogs' command.")
.setColor(util.color.green)
);
}
}
}

module.exports = LogChannelCommand;
54 changes: 54 additions & 0 deletions src/commands/settings/MessageLogChannelCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const Command = require('../../Command');
const util = require('../../util.js');
const Discord = require('discord.js');

class MessageLogChannelCommand extends Command {

static description = 'Configure the channel that messages will be logged in';

static usage = '<#channel|id>|off|status';

static names = ['messagelog','messagelogchannel'];

static userPerms = ['MANAGE_GUILD'];

async execute() {
if (this.args.length !== 1) {
await this.sendUsage();
return;
}

switch (this.args[0].toLowerCase()) {
case "off":
this.guildConfig.messageLogChannel = null;
await this.guildConfig.save();
await this.message.channel.send(new Discord.MessageEmbed()
.setDescription("Disabled message logs")
.setColor(util.color.red)
);
break;
case "status":
await this.message.channel.send(new Discord.MessageEmbed()
.setDescription(`Messages are currently ${this.guildConfig.messageLogChannel ? `logged to <#${this.guildConfig.messageLogChannel}>` :
`not logged.\n Use \`${this.prefix}messagelog ${MessageLogChannelCommand.usage}\` to change this`}.`)
.setColor(this.guildConfig.messageLogChannel ? util.color.green : util.color.red)
);
break;
default:
const channel = util.channelMentionToId(this.args[0]);
if (channel === null || !await util.isChannel(this.message.guild, channel)) return this.sendUsage();
if (!this.message.guild.channels.resolve(/** @type {Snowflake} */channel).permissionsFor(this.bot.user).has(['SEND_MESSAGES', 'VIEW_CHANNEL'])){
return this.message.channel.send('I am missing the required permissions to send messages to that channel!');
}

this.guildConfig.messageLogChannel = channel;
await this.guildConfig.save();
await this.message.channel.send(new Discord.MessageEmbed()
.setDescription(`Set message log channel to <#${channel}>.`)
.setColor(util.color.green)
);
}
}
}

module.exports = MessageLogChannelCommand;
2 changes: 1 addition & 1 deletion src/features/messageDelete/deletion.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports.event = async (options, message) => {
.setFooter(`ID: ${message.author.id}`);
}

await Log.logEmbed(message, embed);
await Log.messageLogEmbed(message, embed);
};

exports.ignore = (id) => {
Expand Down
2 changes: 1 addition & 1 deletion src/features/messageUpdate/edited.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ exports.event = async (options, old, newMsg) => {
)
.setFooter(`ID: ${old.author.id}`);

await Log.logEmbed(old, embed);
await Log.messageLogEmbed(old, embed);
};
31 changes: 31 additions & 0 deletions update/0.4.0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const Database = require('../src/Database');
const config = require('../config.json');
const database = new Database(config.db);
const GuildConfig = require('../src/GuildConfig');

async function update() {
console.log('Starting update to v0.4.0');

console.log('Updating guild log channels')
await database.waitForConnection();
const guilds = await database.queryAll('SELECT id, config FROM guilds');
let updated = 0;

for (const guild of guilds) {
const gc = new GuildConfig(guild.id, guild.config);
if (gc.logChannel) {
gc.messageLogChannel = gc.logChannel;
await gc.save();
updated ++;
}
}

console.log(`Done! Added message log to ${updated} of ${guilds.length} guilds!`);

process.exit(0);
}

update().catch(e => {
console.error(e);
process.exit(1);
});

0 comments on commit 0681be7

Please sign in to comment.