Skip to content

Commit e302c95

Browse files
committed
use the new truncate helper in other places too, for shits and giggles
1 parent dffc1b9 commit e302c95

File tree

5 files changed

+13
-17
lines changed

5 files changed

+13
-17
lines changed

src/bot/commands/moderation/ban.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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, truncate(512, reason));
64+
await message.channel.guild.banMember(user.id, 0, truncate(reason, 512));
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const command = new Command('kick', async (message, args, context) => {
5252

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

src/bot/commands/whois.js

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

77
const {HOST} = process.env;
@@ -156,10 +156,7 @@ const command = new Command('whois', async (message, args, context) => {
156156
notesLine(user.id, message.channel.guild.id, db),
157157
])).join('\n\n');
158158

159-
if (content.length > 2000) {
160-
const endIndicator = '\n\n\u2026'; // ellipsis as one character
161-
content = content.slice(0, 2000 - endIndicator.length) + endIndicator;
162-
}
159+
content = truncate(content, 2000, '\n\n\u2026'); // U+2026 is an ellipsis
163160

164161
message.channel.createMessage({
165162
content,

src/bot/events/rssFeeds.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import log from 'another-logger';
22
import {EventListener} from 'yuuko';
33
import {NewsChannel} from 'eris';
44
import Parser from 'rss-parser';
5+
import {truncate} from '../util/discord';
56
const rssParser = new Parser();
67

78
/**
@@ -11,11 +12,8 @@ const rssParser = new Parser();
1112
* @returns {object}
1213
*/
1314
function buildRssMessageContent (post, feedURL) {
14-
let tempTitle = post.title;
1515
// titles in embeds have a 256 char limit
16-
if (post.title.length > 253) {
17-
tempTitle = post.title.substring(0, 252).concat('...');
18-
}
16+
const tempTitle = truncate(post.title, 256);
1917

2018
// build embed
2119
const contentObject = {

src/bot/util/discord.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,17 @@ export const formatDateRelative = (date: Date) => `<t:${Math.round(date.getTime(
222222
export const AVATAR_IMAGE_SIZE = 512;
223223

224224
/**
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
225+
* Truncates a string to the given number of characters, adding an ellipsis (or
226+
* other indicator) at the end if part of the string had to be cut off.
228227
* @param str The string to be truncated
228+
* @param n The maximum number of characters for the returned string
229+
* @param cutoffIndicator A string added at the end of the result only if the
230+
* input string needed to be cut down. Defaults to a single-character ellipsis.
229231
* @see https://discord.com/developers/docs/resources/audit-log#audit-log-entry-object
230232
*/
231-
export function truncate (n: number, str: string) {
233+
export function truncate (str: string, n: number, cutoffIndicator = '\u2026') {
232234
if (str.length <= n) {
233235
return str;
234236
}
235-
// U+2026 is a single-character ellipsis
236-
return `${str.slice(0, n - 1)}\u2026`;
237+
return `${str.slice(0, n - cutoffIndicator.length)}${cutoffIndicator}`;
237238
}

0 commit comments

Comments
 (0)