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!
Related
First I create an object
var queue = {1:{},2:{},3:{}};
And then I store the message based on QueueKey, or edit if it's already created
if (typeof queue[QueueKey].messageOBJ == 'undefined')
{
queue[QueueKey].messageOBJ = await configChannel.send({ embeds: [getEmbedFloor(QueueKey)] });
}
else
{
queue[QueueKey].messageOBJ = await queue[QueueKey].messageOBJ.edit({ embeds: [getEmbedFloor(QueueKey)] });
}
everything starts working well but after sometime(1~2 hours) bot stops editing the already created message, looks like it lose object reference.
It not pops any error message or code break, seems like the message was edited sucessfully but the real message in discord still the same
I'm thinking in store the messageID instead the whole object and search for the message ID with .fetch() but this will lead to other problems
is there any way to store message Objects properly?
I discovered my problem, actually bot was editing the message to frequently, so after some time discord "auto ban" my bot for some time, something like a cooldown, só it starts to get slower and slower, up to seems like it is stuck.
My solution was check message before edit, to compare if the changes in message are really necessary, before edit or not
I'm relatively new to discord.js, and I've started building a bot project that allows a user to create a message via command, have that message stored in a hidden channel on my private server, and then said message can be extracted through the message ID.
I have the write working and it returns the message ID of the message sent in the hidden channel, but I'm completely stumped on the get command. I've tried searching around online but every method I tried would return errors like "Cannot read property 'fetch' of undefined" or "'channel' is not defined". Here are some examples of what I tried, any help would be appreciated. Note that my args is already accurate, and "args[0]" is the first argument after the command. "COMMAND_CHANNEL" is the channel where the command is being executed while "MESSAGE_DATABASE" is the channel where the targeted message is stored.
let msgValue = channel.messages.cache.get(args[0])
client.channels.cache.get(COMMAND_CHANNEL).send(msgValue.content)
let msgValue = msg.channel.message.fetch(args[0])
.then(message => client.channels.cache.get(COMMAND_CHANNEL).send(msgValue.content))
.catch(console.error);
I even tried using node-fetch to call the discord API itself
const api = require("node-fetch")
let msgValue = api(`https://discordapp.com/api/v8/channels/${MESSAGE_DATABASE}/messages/${args[0]}`)
.then(message => client.channels.cache.get(COMMAND_CHANNEL).send(msgValue.content))
.catch(console.error);
Am I missing something or am I making some sort of mistake?
Edit: Thanks for the help! I finished my bot, it's just a little experimental bot that allows you to create secret messages that can only be viewed through their ID upon executing the command :get_secret_message <message_id>. I posted it on top.gg but it hasn't been approved yet, so in the meantime if anyone wants to mess around with it here is the link: https://discord.com/api/oauth2/authorize?client_id=800368784484466698&permissions=76800&scope=bot
List of commands:
:write_secret_message - Write a secret message, upon execution the bot will DM you the message ID.
:get_secret_message <message_id> - Get a secret message by its ID, upon execution the bot will DM you the message content.
:invite - Get the bot invite link.
NOTE: Your DMs must be turned on or the bot won't be able to DM any of the info.
My test message ID: 800372849155637290
fetch returns the result as promise so you need to use the then to access that value instead of assigning it to a variable (msgValue). Also you made a typo (channel.message -> channel.messages).
I would recommend using something like this:
msg.channel.messages
.fetch(args[0])
.then(message => {
client.channels
.fetch(COMMAND_CHANNEL)
.then(channel => channel.send(message.content))
.catch(console.error)
})
.catch(console.error)
I think you were quite close with the second attempt you posted, but you made one typo and the way you store the fetched message is off.
The typo is you wrote msg.channel.message.fetch(args[0]) but it should be msg.channel.messages.fetch(args[0]) (the typo being the missing s after message). See the messages property of a TextChannel.
Secondly, but this is only a guess really as I can't be sure since you didn't provide much of your code, when you try to fetch the message, you are doing so from the wrong channel. You are trying to fetch the message with a given ID from in the channel the command was executed from (the msg.channel). Unless this command was executed from the "MESSAGE_DATABASE" channel, you would need to fetch the message by ID from the "MESSAGE_DATABASE" channel instead of the msg.channel.
Thirdly, if you fetch a message, the response from the Promise can be used in the .then method. You tried to assign the response to a variable msgValue with let msgValue = msg.channel.message.fetch(args[0]) but that won't do what you'll expect it to do. This will actual assign the entire Promise to the variable. What I think you want to do is just use the respone from the Promise directly in the .then method.
So taking all that, please look at the snippet of code below, with inspiration taken from the MessageManager .fetch examples. Give it a try and see if it works.
// We no longer need to store the value of the fetch in a variable since that won't work as you expect it would.
// Also we're now fetching the message from the MESSAGE_DATABASE channel.
client.channels.cache.get(MESSAGE_DATABASE).fetch(args[0])
// The fetched message will be given as a parameter to the .then method.
.then(fetchedMessage => client.channels.cache.get(COMMAND_CHANNEL).send(fetchedMessage.content))
.catch(console.error);
I am looking to send a message to my logging channel when a user types something important (for a game server) what I have now doesn't work and just spams messages for some reason. if you know how to fix this and make it work and not spam messages, that would be good. (no errors in console)
client.on('message', (message) => {
if (message.content.startsWith("####")) return; {}
const logChannel = client.channels.cache.get('783009285709496371');
logChannel.send(`User: <#${message.author.id}> | Message: ${message.content}`);
})
Invert the return condition. Right now you are sensing all messages that dont start with #####.
Put a ! in front of the content check.
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 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.