Skip to content

Commit

Permalink
Merge pull request #69 from moonstar-x/bugfix/emoji-empty-message
Browse files Browse the repository at this point in the history
Bugfix/emoji empty message
  • Loading branch information
moonstar-x authored Feb 3, 2022
2 parents 29084ce + b43f358 commit 5ccc0ed
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion 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": "discord-tts-bot",
"version": "4.0.5",
"version": "4.0.6",
"description": "A simple Discord TTS Bot that uses the Google Translate TTS API.",
"main": "./src/app.js",
"scripts": {
Expand Down
26 changes: 15 additions & 11 deletions src/classes/tts/TTSChannelHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ class TTSChannelHandler {
}

async handleMessage(message) {
if (message.author.bot || !message.guild) {
return;
}
try {
if (message.author.bot || !message.guild || message.content?.length < 1) {
return;
}

const channelSettings = await this.client.ttsSettings.get(message.channel);
if (!channelSettings || !channelSettings.provider) {
return;
}
const channelSettings = await this.client.ttsSettings.get(message.channel);
if (!channelSettings || !channelSettings.provider) {
return;
}

return this.handleSay(message, channelSettings);
return this.handleSay(message, channelSettings);
} catch (error) {
logger.error(`Something happened when handling the TTS channel ${message.channel.name} with message from ${message.member.displayName}`);
logger.error(error);
}
}

async handleSay(message, channelSettings) {
Expand All @@ -33,14 +38,13 @@ class TTSChannelHandler {

const extras = channelSettings[channelSettings.provider] || TTSPlayer.DEFAULT_SETTINGS[channelSettings.provider];

const { me: { voice: myVoice }, name: guildName, members, channels, roles, emojis } = message.guild;
const { me: { voice: myVoice }, name: guildName, members, channels, roles } = message.guild;
const { channel: memberChannel } = message.member.voice;
const myChannel = myVoice?.channel;
const textToSay = cleanMessage(message.content, {
members: members.cache,
channels: channels.cache,
roles: roles.cache,
emojis: emojis.cache
roles: roles.cache
});

if (!memberChannel) {
Expand Down
5 changes: 2 additions & 3 deletions src/commands/all-tts/SayCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ class SayCommand extends SlashCommand {
const currentSettings = await this.client.ttsSettings.getCurrent(interaction);
const extras = currentSettings[currentSettings.provider] || {};

const { me: { voice: myVoice }, name: guildName, members, channels, roles, emojis } = interaction.guild;
const { me: { voice: myVoice }, name: guildName, members, channels, roles } = interaction.guild;
const { channel: memberChannel } = interaction.member.voice;
const myChannel = myVoice?.channel;
const message = cleanMessage(interaction.options.getString('message'), {
members: members.cache,
channels: channels.cache,
roles: roles.cache,
emojis: emojis.cache
roles: roles.cache
});

if (!memberChannel) {
Expand Down
5 changes: 2 additions & 3 deletions src/commands/google-tts/GoogleSayCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ class GoogleSayCommand extends SlashCommand {
const currentSettings = await this.client.ttsSettings.getCurrent(interaction);
const extras = currentSettings[GoogleProvider.NAME];

const { me: { voice: myVoice }, name: guildName, members, channels, roles, emojis } = interaction.guild;
const { me: { voice: myVoice }, name: guildName, members, channels, roles } = interaction.guild;
const { channel: memberChannel } = interaction.member.voice;
const myChannel = myVoice?.channel;
const message = cleanMessage(interaction.options.getString('message'), {
members: members.cache,
channels: channels.cache,
roles: roles.cache,
emojis: emojis.cache
roles: roles.cache
});

if (!memberChannel) {
Expand Down
5 changes: 2 additions & 3 deletions src/commands/other-tts/SayAeiouCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ class SayAeiouCommand extends SlashCommand {
const ttsPlayer = this.client.getTTSPlayer(interaction.guild);
const connection = ttsPlayer.voice.getConnection();

const { me: { voice: myVoice }, name: guildName, members, channels, roles, emojis } = interaction.guild;
const { me: { voice: myVoice }, name: guildName, members, channels, roles } = interaction.guild;
const { channel: memberChannel } = interaction.member.voice;
const myChannel = myVoice?.channel;
const message = cleanMessage(interaction.options.getString('message'), {
members: members.cache,
channels: channels.cache,
roles: roles.cache,
emojis: emojis.cache
roles: roles.cache
});

if (!memberChannel) {
Expand Down
10 changes: 5 additions & 5 deletions src/utils/mentions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { MessageMentions: { USERS_PATTERN, CHANNELS_PATTERN, ROLES_PATTERN } } = require('discord.js');

const CUSTOM_EMOJI_PATTERN = /<:.*?:(\d{17,19})>/g;
const CUSTOM_EMOJI_PATTERN = /<:(.*?):(\d{17,19})>/g;

const cleanMemberMentions = (message, members) => {
return message.replace(USERS_PATTERN, (_, id) => members.get(id).displayName);
Expand All @@ -14,17 +14,17 @@ const cleanRoleMentions = (message, roles) => {
return message.replace(ROLES_PATTERN, (_, id) => roles.get(id).name);
};

const cleanEmojis = (message, emojis) => {
return message.replace(CUSTOM_EMOJI_PATTERN, (_, id) => emojis.get(id).name);
const cleanEmojis = (message) => {
return message.replace(CUSTOM_EMOJI_PATTERN, (_, name) => name);
};

const cleanMessage = (message, { members, channels, roles, emojis }) => {
const cleanMessage = (message, { members, channels, roles }) => {
let clean = message;

clean = cleanMemberMentions(clean, members);
clean = cleanChannelMentions(clean, channels);
clean = cleanRoleMentions(clean, roles);
clean = cleanEmojis(clean, emojis);
clean = cleanEmojis(clean);

return clean;
};
Expand Down

0 comments on commit 5ccc0ed

Please sign in to comment.