Skip to content

Commit

Permalink
Fix buttons with custom emojis if no emojis are configured
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Jan 20, 2024
1 parent e685b0f commit 1864933
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 45 deletions.
39 changes: 25 additions & 14 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": "3.4.4",
"version": "3.4.5",
"description": "Discord Bot for the Aternos Discord server",
"main": "index.js",
"type": "module",
Expand Down
5 changes: 3 additions & 2 deletions src/commands/settings/SettingsOverviewCommand.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import SubCommand from '../SubCommand.js';
import GuildSettings from '../../settings/GuildSettings.js';
import {ActionRowBuilder, ButtonBuilder, ButtonStyle} from 'discord.js';
import config from '../../bot/Config.js';
import {componentEmojiIfExists} from '../../util/format.js';
import icons from '../../util/icons.js';

export default class SettingsOverviewCommand extends SubCommand {

Expand Down Expand Up @@ -35,7 +36,7 @@ export default class SettingsOverviewCommand extends SubCommand {
.setLabel('Refresh')
.setStyle(ButtonStyle.Secondary)
.setCustomId('settings:overview')
.setEmoji(config.data.emoji.refresh ?? {})
.setEmoji(componentEmojiIfExists('refresh', icons.refresh))
)
]
};
Expand Down
43 changes: 22 additions & 21 deletions src/commands/user/UserInfoCommand.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Command from '../Command.js';
import {ActionRowBuilder, ButtonBuilder, ButtonStyle, PermissionFlagsBits, PermissionsBitField, time, TimestampStyles} from 'discord.js';
import {ActionRowBuilder, ButtonStyle, PermissionFlagsBits, PermissionsBitField, time, TimestampStyles} from 'discord.js';
import GuildWrapper from '../../discord/GuildWrapper.js';
import MemberWrapper from '../../discord/MemberWrapper.js';
import UserWrapper from '../../discord/UserWrapper.js';
import colors from '../../util/colors.js';
import UserEmbed from '../../embeds/UserEmbed.js';
import ErrorEmbed from '../../embeds/ErrorEmbed.js';
import {componentEmojiIfExists, inlineEmojiIfExists} from '../../util/format.js';
import BetterButtonBuilder from '../../embeds/BetterButtonBuilder.js';

export default class UserInfoCommand extends Command {

Expand Down Expand Up @@ -74,39 +75,39 @@ export default class UserInfoCommand extends Command {
/** @type {ActionRowBuilder<ButtonBuilder>} */
const actionRow = new ActionRowBuilder()
.addComponents(
/** @type {*} */ new ButtonBuilder()
/** @type {*} */ new BetterButtonBuilder()
.setLabel('Strike')
.setCustomId(`strike:${user.id}`)
.setStyle(ButtonStyle.Danger)
.setEmoji(componentEmojiIfExists('strike')),
.setEmojiIfPresent(componentEmojiIfExists('strike')),
);
const informationRow = new ActionRowBuilder()
.addComponents(
/** @type {*} */ new ButtonBuilder()
/** @type {*} */ new BetterButtonBuilder()
.setLabel('Refresh')
.setCustomId(`user:refresh:${user.id}`)
.setStyle(ButtonStyle.Secondary)
.setEmoji(componentEmojiIfExists('refresh')),
/** @type {*} */ new ButtonBuilder()
.setEmojiIfPresent(componentEmojiIfExists('refresh')),
/** @type {*} */ new BetterButtonBuilder()
.setLabel('Avatar')
.setCustomId(`avatar:${user.id}`)
.setStyle(ButtonStyle.Secondary)
.setEmoji(componentEmojiIfExists('avatar')),
/** @type {*} */ new ButtonBuilder()
.setEmojiIfPresent(componentEmojiIfExists('avatar')),
/** @type {*} */ new BetterButtonBuilder()
.setLabel('Moderations')
.setCustomId(`moderation:list:${user.id}`)
.setStyle(ButtonStyle.Secondary)
.setEmoji(componentEmojiIfExists('moderations')),
.setEmojiIfPresent(componentEmojiIfExists('moderations')),
);

if (member) {
embed.addPair(inlineEmojiIfExists('userJoined') + 'Joined', time(member.joinedAt, TimestampStyles.LongDate));
actionRow.addComponents(
/** @type {*} */ new ButtonBuilder()
/** @type {*} */ new BetterButtonBuilder()
.setLabel('Kick')
.setCustomId(`kick:${user.id}`)
.setStyle(ButtonStyle.Danger)
.setEmoji(componentEmojiIfExists('kick'))
.setEmojiIfPresent(componentEmojiIfExists('kick'))
);
}

Expand All @@ -116,11 +117,11 @@ export default class UserInfoCommand extends Command {
.addPair(inlineEmojiIfExists('strike') + ' Strike count', strikeCount);
if (strikeCount) {
actionRow.addComponents(
/** @type {*} */ new ButtonBuilder()
/** @type {*} */ new BetterButtonBuilder()
.setLabel('Pardon')
.setCustomId(`pardon:${user.id}`)
.setStyle(ButtonStyle.Success)
.setEmoji(componentEmojiIfExists('pardon'))
.setEmojiIfPresent(componentEmojiIfExists('pardon'))
);
}
}
Expand All @@ -133,21 +134,21 @@ export default class UserInfoCommand extends Command {
embed.addPair(inlineEmojiIfExists('mute') + 'Muted until', time(Math.floor(mute.end / 1_000)));
}
actionRow.addComponents(
/** @type {*} */ new ButtonBuilder()
/** @type {*} */ new BetterButtonBuilder()
.setLabel('Unmute')
.setCustomId(`unmute:${user.id}`)
.setStyle(ButtonStyle.Success)
.setEmoji(componentEmojiIfExists('mute'))
.setEmojiIfPresent(componentEmojiIfExists('mute'))
);
embed.setColor(colors.ORANGE);
}
else {
actionRow.addComponents(
/** @type {*} */ new ButtonBuilder()
/** @type {*} */ new BetterButtonBuilder()
.setLabel('Mute')
.setCustomId(`mute:${user.id}`)
.setStyle(ButtonStyle.Danger)
.setEmoji(componentEmojiIfExists('mute'))
.setEmojiIfPresent(componentEmojiIfExists('mute'))
);
}
}
Expand All @@ -160,21 +161,21 @@ export default class UserInfoCommand extends Command {
embed.addPair(inlineEmojiIfExists('ban') + 'Banned until', time(Math.floor(ban.end / 1_000)));
}
actionRow.addComponents(
/** @type {*} */ new ButtonBuilder()
/** @type {*} */ new BetterButtonBuilder()
.setLabel('Unban')
.setCustomId(`unban:${user.id}`)
.setStyle(ButtonStyle.Success)
.setEmoji(componentEmojiIfExists('ban'))
.setEmojiIfPresent(componentEmojiIfExists('ban'))
);
embed.setColor(colors.RED);
}
else {
actionRow.addComponents(
/** @type {*} */ new ButtonBuilder()
/** @type {*} */ new BetterButtonBuilder()
.setLabel('Ban')
.setCustomId(`ban:${user.id}`)
.setStyle(ButtonStyle.Danger)
.setEmoji(componentEmojiIfExists('ban'))
.setEmojiIfPresent(componentEmojiIfExists('ban'))
);
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/embeds/BetterButtonBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {ButtonBuilder} from 'discord.js';

export default class BetterButtonBuilder extends ButtonBuilder {
/**
* Set the emoji for this button.
* If the emoji parameter is null, don't change the emoji.
*
* @param {?import('discord.js').ComponentEmojiResolvable} emoji
* @return {ButtonBuilder|BetterButtonBuilder}
*/
setEmojiIfPresent(emoji) {
if (!emoji) {
return this;
}

return super.setEmoji(emoji);
}
}
17 changes: 10 additions & 7 deletions src/util/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,19 @@ export function formatNumber(number, name) {
}

/**
* @param {string} configKey
* @param {?string} fallback
* @return {import('discord.js').APIMessageComponentEmoji}
* @param {string} configKey name of the emoji in the config
* @param {?string} fallback emoji character to use if the config key is not set
* @return {?import('discord.js').APIMessageComponentEmoji}
*/
export function componentEmojiIfExists(configKey, fallback = null) {
const emoji = config.data.emoji[configKey];
if (!emoji) {
return {name: fallback ?? undefined};
}
else {
if (emoji) {
return {id: emoji};
}

if (fallback) {
return {name: fallback};
}

return null;
}

0 comments on commit 1864933

Please sign in to comment.