'Discord.js bot crashes with permission error
There are two servers that I'm testing on, and on one server, the bot works, but the bot does not work on another server.
However, the ping command works
The command that makes the bot crash on only one server, while it works on another server
import { MessageActionRow, MessageButton, MessageEmbed } from 'discord.js';
export const data = {
name: 'ticket',
description: 'Makes a ticket',
};
const actionRow = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('create-ticket')
.setLabel('티켓 생성하기')
.setEmoji('🎟️')
.setStyle('PRIMARY'),
);
export async function onRun(interaction) {
const embed = new MessageEmbed()
.setColor('#0099ff')
.setTitle('티켓 생성')
.setDescription('어쩌고 저쩌고');
await interaction.channel.send({ embeds: [embed], components: [actionRow] });
}
This is the ping command that works
import { MessageEmbed } from 'discord.js';
import { client } from '../bot.js';
export const data = {
name: 'ping',
description: 'Ping Pong!',
};
export async function onRun(interaction) {
const message = await interaction.reply({ content: 'Pinging...', fetchReply: true });
const embed = new MessageEmbed()
.setTitle('Pong!')
.addField('Message Latency', `${(message.createdTimestamp || Date.parse(message.timestamp)) - interaction.createdTimestamp}ms`)
.addField('Discord Latency', `${client.ws.ping}ms`);
await interaction.editReply({ embeds: [embed], content: 'Pong!' });
}
Edit: The error Discord says the interaction failed, and the bot crashes with the following log
C:\Users\issac\WebstormProjects\discord-bot\node_modules\discord.js\src\rest\RequestHandler.js:350
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Missing Permissions
at RequestHandler.execute (C:\Users\issac\WebstormProjects\discord-bot\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (C:\Users\issac\WebstormProjects\discord-bot\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
at async TextChannel.send (C:\Users\issac\WebstormProjects\discord-bot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:175:15)
at async Module.onRun (file:///C:/Users/issac/WebstormProjects/discord-bot/src/commands/ticket.js:22:5) {
method: 'post',
path: '/channels/950311913391259659/messages',
code: 50013,
httpStatus: 403,
requestData: {
json: {
content: undefined,
tts: false,
nonce: undefined,
embeds: [
{
title: '티켓 생성',
type: 'rich',
description: '어쩌고 저쩌고',
url: null,
timestamp: 0,
color: 39423,
fields: [],
thumbnail: null,
image: null,
author: null,
footer: null
}
],
components: [ { components: [ [Object] ], type: 1 } ],
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: undefined,
message_reference: undefined,
attachments: undefined,
sticker_ids: undefined
},
files: []
}
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Solution 1:[1]
This means that your bot is missing permissions to (presumably) send messages on that server.
You can prevent it from crashing by adding a .catch statement after sending the message like this:
const message = await interaction.reply({ content: 'Pinging...', fetchReply: true }).catch((e) => console.log(e))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Aci |
