Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix commands that send messages in DMs crashing the entire bot #248

Merged
merged 3 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 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
Expand Up @@ -16,7 +16,7 @@
"connect-mongo": "^3.2.0",
"convert-units": "^2.3.4",
"dotenv": "^16.3.1",
"eris": "^0.17.0",
"eris": "^0.17.2",
"express-session": "^1.17.0",
"mongodb": "^3.5.5",
"node-fetch": "^2.6.7",
Expand Down
8 changes: 4 additions & 4 deletions src/bot/commands/misc/avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ function resizeAvatarURL (url, size) {
return `${url.split('?size=')[0]}?size=${size}`;
}

const command = new Command('avatar', async (msg, args) => {
const command = new Command('avatar', async (msg, args, {sendMessage}) => {
if (args.length === 0) {
msg.channel.createMessage(resizeAvatarURL(msg.member.avatarURL, AVATAR_IMAGE_SIZE)).catch(() => {});
sendMessage(msg, resizeAvatarURL(msg.member.avatarURL, AVATAR_IMAGE_SIZE)).catch(() => {});
return;
}
const [member] = await parseGuildMember(args.join(' '), msg.channel.guild, msg.member);
if (!member) {
msg.channel.createMessage('Member not found, check if what you\'re using is correct or try using an ID.').catch(() => {});
sendMessage(msg, 'Member not found, check if what you\'re using is correct or try using an ID.').catch(() => {});
return;
}

msg.channel.createMessage(resizeAvatarURL(member.avatarURL, AVATAR_IMAGE_SIZE)).catch(() => {});
sendMessage(msg, resizeAvatarURL(member.avatarURL, AVATAR_IMAGE_SIZE)).catch(() => {});
});
command.help = {
args: '[user]',
Expand Down
2 changes: 1 addition & 1 deletion src/bot/commands/misc/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const command = new Command('convert', async (msg, args, context) => {
}
}

msg.channel.createMessage(message).catch(() => {});
context.sendMessage(msg, message).catch(() => {});
});
command.help = {
args: '[amount] <source currency or unit> <destination currency or unit>',
Expand Down
8 changes: 4 additions & 4 deletions src/bot/commands/misc/defaultavatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import {Command} from 'yuuko';
import {AVATAR_IMAGE_SIZE, parseUser} from '../../util/discord';

const command = new Command('defaultavatar', async (msg, args) => {
const command = new Command('defaultavatar', async (msg, args, {sendMessage}) => {
if (args.length === 0) {
msg.channel.createMessage(msg.author.dynamicAvatarURL('', AVATAR_IMAGE_SIZE)).catch(() => {});
sendMessage(msg, msg.author.dynamicAvatarURL('', AVATAR_IMAGE_SIZE)).catch(() => {});
return;
}
const [user] = await parseUser(args.join(' '), msg.channel.guild, msg.author);
if (!user) {
msg.channel.createMessage('Member not found, check if what you\'re using is correct or try using an ID.').catch(() => {});
sendMessage(msg, 'Member not found, check if what you\'re using is correct or try using an ID.').catch(() => {});
return;
}

msg.channel.createMessage(user.dynamicAvatarURL('', AVATAR_IMAGE_SIZE)).catch(() => {});
sendMessage(msg, user.dynamicAvatarURL('', AVATAR_IMAGE_SIZE)).catch(() => {});
});
command.help = {
args: '[user]',
Expand Down
6 changes: 3 additions & 3 deletions src/bot/commands/misc/joke.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import {Command} from 'yuuko';
import fetch from 'node-fetch';

const command = new Command('joke', async msg => {
const command = new Command('joke', async (msg, _, {sendMessage}) => {
try {
const res = await fetch('https://official-joke-api.appspot.com/random_joke');
if (res.status !== 200) {
throw new Error('Error getting joke.');
}
const joke = await res.json();
msg.channel.createMessage(`${joke.setup}\n${joke.punchline}`);
sendMessage(msg, `${joke.setup}\n${joke.punchline}`);
} catch (err) {
msg.channel.createMessage(err.message).catch(() => {});
sendMessage(msg, err.message).catch(() => {});
}
});
command.help = {
Expand Down
2 changes: 1 addition & 1 deletion src/bot/commands/misc/pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const command = new Command('pick', (msg, args, context) => {
// We use a comma as a separator for each choice
const choices = args.join(' ').split(',').map(str => str.trim());

msg.channel.createMessage(`I pick **${choices[getRandomInt(0, choices.length - 1)]}**`).catch(() => {});
context.sendMessage(msg, `I pick **${choices[getRandomInt(0, choices.length - 1)]}**`).catch(() => {});
});
command.help = {
args: '<choice>, <choice>, <more choices...>',
Expand Down
6 changes: 3 additions & 3 deletions src/bot/commands/misc/roll.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

const command = new Command('roll', (msg, args) => {
const command = new Command('roll', (msg, args, {sendMessage}) => {
let min = 0;
let max = 6;

Expand All @@ -18,9 +18,9 @@ const command = new Command('roll', (msg, args) => {
}

if (isNaN(min) || isNaN(max)) {
msg.channel.createMessage('Please enter a valid number!').catch(() => {});
sendMessage(msg, 'Please enter a valid number!').catch(() => {});
} else {
msg.channel.createMessage(`You rolled a ${getRandomInt(min, max)}`).catch(() => {});
sendMessage(msg, `You rolled a ${getRandomInt(min, max)}`).catch(() => {});
}
});
command.help = {
Expand Down
4 changes: 2 additions & 2 deletions src/bot/commands/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if (!gitRepoURI) {
});
}

const command = new Command('ping', async (msg, args, {prefix, client}) => {
const command = new Command('ping', async (msg, args, {prefix, client, sendMessage}) => {
let messageContent = 'Pong!';
if (commitHash != null) {
messageContent += `\nCommit: ${commitHash}`;
Expand All @@ -43,7 +43,7 @@ const command = new Command('ping', async (msg, args, {prefix, client}) => {
messageContent += `\nUse \`${prefix.match(client.mentionPrefixRegExp) ? '' : prefix}help\` for a list of commands.`;

const then = Date.now();
const newMsg = await msg.channel.createMessage(messageContent);
const newMsg = await sendMessage(msg, messageContent);
newMsg.edit(newMsg.content.replace('Pong!', `Pong! (${Date.now() - then}ms REST round-trip)`));
});
command.help = {
Expand Down
2 changes: 1 addition & 1 deletion src/bot/commands/reminders/remind.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const command = new Command(['remind', 'remindme'], async (message, args, contex
}

// Send confirmation, throw away any possible errors
message.channel.createMessage(`Will remind you ${formatDateRelative(due)}.`).catch(() => {});
context.sendMessage(message, `Will remind you ${formatDateRelative(due)}.`).catch(() => {});
});
command.help = {
desc: "Sets a reminder that you'll get pinged for in the future.",
Expand Down
8 changes: 8 additions & 0 deletions src/bot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ export default (mongoClient, db) => {
},
});

// Another helper for sending messages because discord is wack and stopped
// giving us DM channel objects to send messages in
bot.extendContext({
sendMessage (msg, ...args) {
return bot.createMessage(msg.channel.id, ...args);
},
});

// Connect the bot to Discord
bot.connect();

Expand Down