Skip to content

Commit dffc1b9

Browse files
committed
limit audit log reasons for kicks and bans to 512 characters
1 parent ceedc7a commit dffc1b9

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/bot/commands/moderation/ban.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import createLogger from 'another-logger';
22
const log = createLogger({label: 'command:ban'});
33
import {Command} from 'yuuko';
4-
import {parseUser, parseTime, formatDateRelative, awaitReaction} from '../../util/discord';
4+
import {parseUser, parseTime, formatDateRelative, awaitReaction, truncate} from '../../util/discord';
55
import {escape, blockquote} from '../../util/formatting';
66

77
const confirmationEmoji = '🔨';
@@ -61,7 +61,7 @@ const command = new Command('ban', async (message, args, context) => {
6161
try {
6262
// TODO: check if member is already banned before banning again
6363
// TODO: service to clear bans after they expire
64-
await message.channel.guild.banMember(user.id, 0, reason);
64+
await message.channel.guild.banMember(user.id, 0, truncate(512, reason));
6565
} catch (error) {
6666
log.error(`Error banning user ${user.id} from guild ${message.channel.guild.id}:`, error);
6767

src/bot/commands/moderation/kick.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import createLogger from 'another-logger';
22
const log = createLogger({label: 'command:kick'});
33
import {Command} from 'yuuko';
4-
import {parseGuildMember, awaitReaction} from '../../util/discord';
4+
import {parseGuildMember, awaitReaction, truncate} from '../../util/discord';
55
import {escape, blockquote} from '../../util/formatting';
66

77
const confirmationEmoji = '👢';
@@ -52,7 +52,7 @@ const command = new Command('kick', async (message, args, context) => {
5252

5353
// Kick the member
5454
try {
55-
await member.kick(reason);
55+
await member.kick(truncate(512, reason));
5656
} catch (error) {
5757
log.error(`Error kicking user ${member.id} from guild ${message.channel.guild.id}:`, error);
5858

src/bot/util/discord.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,18 @@ export const formatDateRelative = (date: Date) => `<t:${Math.round(date.getTime(
220220

221221
/** Size of avatars shown from the avatar and defaultavatar command */
222222
export const AVATAR_IMAGE_SIZE = 512;
223+
224+
/**
225+
* Truncates a string to the given number of characters, adding an ellipsis
226+
* at the end if part of the string had to be cut off.
227+
* @param n The maximum number of characters for the returned string
228+
* @param str The string to be truncated
229+
* @see https://discord.com/developers/docs/resources/audit-log#audit-log-entry-object
230+
*/
231+
export function truncate (n: number, str: string) {
232+
if (str.length <= n) {
233+
return str;
234+
}
235+
// U+2026 is a single-character ellipsis
236+
return `${str.slice(0, n - 1)}\u2026`;
237+
}

0 commit comments

Comments
 (0)