I wanted to make it so everytime my bot had an error it would send the error in a channel but it does nothing
bot.on('error', function (err) {
bot.guilds.get("609118791854456860").channels.get("609118791854456865").send(err)
})
I don't believe Client emits an event called "error". This code here should catch all uncaught errors and send them in a channel of your choosing:
process.on("uncaughtException", e => {
console.error(e);
Client.channels.get("YOUR CHANNEL ID").send(e.stack.slice(0, 2000); //ensure the stack trace is not too long, messages are limited to 2000 characters
process.exit();
});
In this code snippet, I've named my new Discord.Client() instance Client, it seems you've named yours bot, so you can swap the two names.
According to the docs, the Error Event is calledwhenever the client's WebSocket encounters a connection error.I believe the key is connection error. So if this event is called, you are no longer connected or something is wrong with the connection. Therefore no message can be sent if it can't connect.
One workaround is what Cloud has in his answer, and use the process.on("uncaughtException", e => {})
But just in case the error is fatal and the bot can't connect. You should save the error to a .txt file so whenever the bot successfully re-connects, you send whatever is in that file to your desired channel. Then have the bot delete the file if it successfully sent the message.
Related
So I have a order system that sends embeds incl all important information into a channel.
Whenever an order is started that embed then goes into "In progress" status by changing its buttons and adding a "Complete" button which then marks the order as completed.
In total the message is sent and edited twice.
Now here's the issue: If the bot ever crashes, due to a server reboot or similar, editing the messages it sent prior to the crash will yield the error:
(node:6672) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot edit a message authored by another user
Now I can't just go and check if author.id === client.id because that'll obviously just return true. So how can I fix that issue? As of now I decided to just add a try catch but I'd rather just add an if clause at the top of the event.
DiscordAPIError: Cannot edit a message authored by another user means you are trying to edit another user message
Here's the code for editing bot message:
const msg = await message.channel.send("Will be edit..")
setTimeout(function() {
msg.edit("Edited")
}, 1000) // will edit the message after 1000 ms (1 second)
So im making a discord bot, i made a system so if the bot joins a server it sends a message with the server name in the developer server. but i want it to delete the message when it leaves the server. idk how to do that.
You can create a listener that is triggered whenever a user sends a message. In that listener you would check the message.content against your prohibited word(s) and delete it. You can do that like so:
client.on('messageCreate', () => {
if(message.content.includes("Your prohibited word(s)") {
message.delete()
}
}
I made a code in which he sends a message to people with a specific role, however, for some reason it is sending only to me, code below
canal.send(`||#everyone||`, embed).then(message => {
message.react('748309940795473980')
})
servidor.roles.cache.get("771492474594000928").members.forEach((membro) => {
console.log(membro.user.username)
membro.send(`${membro}`, embed)
})
I would like you to help me with this, this command does the following functions:
Send Message to One Channel (Working)
Send Message to People with a Specific Position (Not Working, Just Send to Me)
As You Can See, I tried to put console.log, however, he was supposed to appear the name of everyone who received the message, but no, only my name appears, and only I get the message
I Need to Get This Working!
I’m trying to make my bot send a message to a channel whenever a user deletes his/her message, sorta like the bot Dyno, but I do not know how to do this. I think the method is .deleted() but I can’t seem to make it work. Can anyone tell me how? Sorry for lack of detail, there’s nothing else to add. Thank you in advance.
The Client (or Bot) has an event called messageDelete which is fired everytime a message is deleted. The given parameter with this event is the message that has been deleted. Take a look at the sample code below for an example.
// Create an event listener for deleted messages
client.on('messageDelete', message => {
// Fetch the designated channel on a server
const channel = message.guild.channels.cache.find(ch => ch.name === 'deleted-messages-log');
// Do nothing if the channel wasn't found on this server
if (!channel) return;
// Send a message in the log channel
channel.send(`A message has been deleted. The message was: ${message.content}`);
});
I'm making a bot which on a react to a certain will create a channel...
That all works perfectly expect I want a nessage to be posted when the cahnnel is created which has a specfic beginning.
client.on('channelCreate', (channel, message) => {
if(channel.name.startsWith('ticket-')){
message.channel.send('test');
});
I'm not getting any errors, just nothing...
You can't use the message variable in the channelCreate event. The only thing you're receiving is a channel object, so you need to use channel.send():
client.on('channelCreate', (channel, message) => {
if(channel.name.startsWith('ticket-')){
channel.send('test');
});