Skip to content

Commit

Permalink
Merge pull request #329 from aternosorg/joinlogs
Browse files Browse the repository at this point in the history
Add join logs
  • Loading branch information
JulianVennen authored Aug 9, 2021
2 parents 3ac102e + cec9ae8 commit 89e5d30
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/Bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class Bot {
constructor() {
this.#client = new Discord.Client({
disableMentions: 'everyone',
presence: { status: "dnd", activity: { type: "WATCHING", name: "you" } }
presence: { status: 'dnd', activity: { type: 'WATCHING', name: 'you' } },
partials: ['GUILD_MEMBER']
});

this.#database = new Database(config.db);
Expand All @@ -40,7 +41,7 @@ class Bot {
await this.#monitor.notice('Starting modbot');
await this.#database.waitForConnection();
await this.#monitor.info('Connected to database!');
console.log("Connected!");
console.log('Connected!');

await this.#database.createTables();
util.init(this.#database, this.#client);
Expand Down Expand Up @@ -94,7 +95,7 @@ class Bot {
try {
features.push(require(path));
} catch (e) {
await this.#monitor.critical(`Failed to load feature '${folder}/${file}'`, e)
await this.#monitor.critical(`Failed to load feature '${folder}/${file}'`, e);
console.error(`Failed to load feature '${folder}/${file}'`, e);
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/Log.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ class Log{
return this.messageLog(guildInfo,'',new Discord.MessageEmbed(embed));
}

/**
* Logs a message to the guilds join 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 joinLog(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.joinLogChannel) return null;

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

/**
* Log a moderation
* @async
Expand Down
54 changes: 54 additions & 0 deletions src/commands/settings/JoinLogChannelCommand.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 JoinLogChannelCommand extends Command {

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

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

static names = ['joinlog','memberlog'];

static userPerms = ['MANAGE_GUILD'];

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

switch (this.args[0].toLowerCase()) {
case 'off':
this.guildConfig.joinLogChannel = null;
await this.guildConfig.save();
await this.message.channel.send(new Discord.MessageEmbed()
.setDescription('Disabled join logs')
.setColor(util.color.red)
);
break;
case 'status':
await this.message.channel.send(new Discord.MessageEmbed()
.setDescription(`New members are currently ${this.guildConfig.joinLogChannel ? `logged to <#${this.guildConfig.joinLogChannel}>` : 'not logged'}.`)
.setColor(this.guildConfig.joinLogChannel ? 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.joinLogChannel = channel;
await this.guildConfig.save();
await this.message.channel.send(new Discord.MessageEmbed()
.setDescription(`Set join log channel to <#${channel}>.`)
.setColor(util.color.green)
);
}
}
}
}

module.exports = JoinLogChannelCommand;
4 changes: 3 additions & 1 deletion src/config/GuildConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class GuildConfig extends Config {
* @param {Snowflake} id guild id
* @param {Object} [json] options
* @param {Snowflake} [json.logChannel] id of the log channel
* @param {Snowflake} [json.messageLogChannel] if of the message log channel
* @param {Snowflake} [json.messageLogChannel] id of the message log channel
* @param {Snowflake} [json.joinLogChannel] id of the join log channel
* @param {Snowflake} [json.mutedRole] id of the muted role
* @param {Snowflake[]} [json.modRoles] role ids that can execute commands
* @param {Snowflake[]} [json.protectedRoles] role ids that can't be targeted by moderations
Expand All @@ -44,6 +45,7 @@ class GuildConfig extends Config {

this.logChannel = json.logChannel;
this.messageLogChannel = json.messageLogChannel;
this.joinLogChannel = json.joinLogChannel;
this.mutedRole = json.mutedRole;
if (json.modRoles instanceof Array)
this.#modRoles = json.modRoles;
Expand Down
21 changes: 21 additions & 0 deletions src/features/guildMemberAdd/logJoin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const Log = require('../../Log');
const {MessageEmbed} = require('discord.js');
const util = require('../../util');

/**
* @param options
* @param {module:"discord.js".GuildMember} member
* @return {Promise<void>}
*/
exports.event = async (options, member) => {
let description = `**ID:** ${member.id}\n` +
`**Created Account:** <t:${Math.floor(member.user.createdTimestamp / 1000)}:R>\n`;

await Log.joinLog(member.guild.id, '', new MessageEmbed()
.setTitle(`${member.user.tag} joined this server`)
.setColor(util.color.green)
.setThumbnail(member.user.avatarURL())
.setDescription(description)
.setTimestamp()
);
};
13 changes: 12 additions & 1 deletion src/features/guildMemberAdd/restoreMuted.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
const Log = require('../../Log');
const GuildConfig = require('../../config/GuildConfig');
const util = require('../../util');
const {APIErrors} = require('discord.js').Constants;

exports.event = async (options, member) => {
let result = await options.database.query('SELECT * FROM moderations WHERE action = \'mute\' AND active = TRUE AND userid = ? AND guildid = ?',[member.id,member.guild.id]);
if (result) {
let guildConfig = await GuildConfig.get(member.guild.id);
await member.roles.add(guildConfig.mutedRole);

try {
await member.roles.add(guildConfig.mutedRole);
}
catch (e) {
if ([APIErrors.UNKNOWN_MEMBER, APIErrors.UNKNOWN_ROLE].includes(e.code)) {
return;
}
throw e;
}

await Log.logEmbed(member.guild,{
title: `Restored mute | ${util.escapeFormatting(member.user.tag)}`,
description: `Mute ID: ${result.id}`,
Expand Down
25 changes: 25 additions & 0 deletions src/features/guildMemberRemove/logLeft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Log = require('../../Log');
const {MessageEmbed} = require('discord.js');
const util = require('../../util');

/**
* @param options
* @param {module:"discord.js".GuildMember} member
* @return {Promise<void>}
*/
exports.event = async (options, member) => {
let description = `**ID:** ${member.id}\n` +
`**Created Account:** <t:${Math.floor(member.user.createdTimestamp / 1000)}:R>\n`;

if (member.joinedTimestamp) {
description += `**Joined:** <t:${Math.floor(member.joinedTimestamp / 1000)}:R>`;
}

await Log.joinLog(member.guild.id, '', new MessageEmbed()
.setTitle(`${member.user.tag} left this server`)
.setColor(util.color.red)
.setThumbnail(member.user.avatarURL())
.setDescription(description)
.setTimestamp()
);
};
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ async function messagesAfter(channel, message, limit) {
util.ignoresAutomod = async (message) => {
/** @type {GuildConfig} */
const guildconfig = await GuildConfig.get(message.guild.id);
return message.author.bot || message.member.hasPermission('MANAGE_MESSAGES') || guildconfig.isProtected(message.member);
return message.system || message.author.bot || message.member.hasPermission('MANAGE_MESSAGES') || guildconfig.isProtected(message.member);
};

/**
Expand Down

0 comments on commit 89e5d30

Please sign in to comment.