Discord.js : Move message to bottom of Text-Channel - javascript

I want to move a specific message (identified by an ID) to the bottom of the text-channel it is in. As if it was newly posted. (This will be triggered by command)
Pinning is not a solution, because that does not pin the message to the bottom of the chat as one might think. It just adds it to a list of pinned messages, only visible by clicking the pin icon, which pretty much noone does unless they're told to.
Reposting the message isn't a solution, because it has a lot of User-Reactions under it, which should remain. I have not yet found a way to transfer all reactions from one message to another, and, looking at the Discord.js Documentation, I don't think it is possible. You can't even fake-react as a user, let alone add multiple reactions at once.
Howevery simply moving a message to the bottom of the text-channel might be possible, although i cannot find a method for it either. But maybe I've just overlooked something.
Is it possible, and if yes, how?

Seeing what you need the only possible solution is the one I propose below, since discord does not allow you to do what you want to do literally.
Something like this but answered by the bot:
Using discord-reply: https://www.npmjs.com/package/discord-reply
const discord = require('discord.js');
require('discord-reply'); //⚠️ IMPORTANT: put this before your discord.Client()
const client = new discord.Client();
client.on('ready', () => {
console.log(client.user.tag)
});
client.on('message', async message => {
if (message.content.startsWith('!reply')) {
message.lineReply('Hey'); //Line (Inline) Reply with mention
message.lineReplyNoMention(`My name is ${client.user.username}`); //Line (Inline) Reply without mention
}
});
client.login('TOKEN');
On command handler
/**
* No need to define it
* */
module.exports = {
name: 'reply',
category: 'Test',
run: (client, message, args) => {
message.lineReply('This is reply with #mention');
}
}
Credits: discord.js | Reply to Message (Actual Reply with Reply Decoration

Related

Discord.js Bot can't handle multiple buttons in the same channel (Version 13)

Is there a way to have a command with buttons be used by two people at the same time in the same channel?
I made an adventure system which is going well so far, but I found out that only one person can use it in a channel. If another person uses it in the same channel, the "DiscordAPIError: Unknown Interaction" error comes as soon as a button is used. I tried adding the message author's id to the custom ID of the buttons, but it still doesn't work for some reason and I am utterly confused.
From what I understand, I think it's unrelated to there being multiple button instances, but I can't think of any other reason. Here is the filter:
const filter = async (interaction) => {
if (interaction.user.id === message.author.id) return true;
await interaction.deferReply()
await interaction.editReply({ content: `**${interaction.user.username}**, You can't use this button!`, ephemeral: true})
return false
}
And the button click detector goes something like this:
GameCollect.on('collect', async ButtonInteraction => {
ButtonInteraction.deferUpdate()
// My code
})
I have tried:
Removing ButtonInteraction.deferUpdate() from all my click collectors.
Making the custom ids for the buttons have the message author's id at the end and making my code compatible with that change.
Smashing my head on my keyboard from confusion. Didn't work either... (satire)
If it's necessary, I can copy my code into a repl so that you can identify the problem easier, but I don't think the other portions would have any effect on this. I think it's just a piece of code/slight adjustment that I have to add for it to work with two people in the same channel.
EDIT: Alright, here is a source bin so that you can identify the problem better. Check the comments on the recent answer for some clarifications on my testing too. I will keep you updated if I find a solution.
DISCLAIMER: I still haven't found a way to make this work in over a month, and I am essentially just giving up on buttons. Thank you to everyone who helped though.
Try it:
Remove await interaction.deferReply() from filter
Create dynamic customIds like Math.random()
EDIT:
description: i had the same issue recently on my own bot, The problem is that when 2 messages that the have collectors(basically they're waiting for a interaction), if you call await interaction.deferReply() soon, it wil get messy, So here's the solution
First of all lets go for the filter function for checking the interaction, We need 2 things:
Checking the button id with the incoming interaction button id
Checking the user to be the real one
After all of these
delete deferReply from the filter func, Because when you didn't check the interactions, calling deferReply, will call both of messages or all of the messages that are waiting for interaction
add the deferReply to the function that is after filter
And don't remember to use random ids for components(Buttons, select menus and ...)
Instead of creating a collector on the text channel, you should create the collector on the actual message
const GameCollect = message.channel.createMessageComponentCollector({ filter, time: 60000 * 15 })
To
const GameCollect = <Message>.createMessageComponentCollector({ filter, time: 60000 * 15 })

Discord.js - Get Message from Interaction

client.ws.on('INTERACTION_CREATE', async (interaction) => {}
The interaction class has lots of properties like channel, member and id but it doesn't have a message property.
Is there a way to get the message from an interaction or will I have to use a event listener on message? And if so, how would I use this with slash commands?
You can get the user input by just using the base class interaction. However, the content is not visible, but you can view it by passing it through an api endpoint or something similar, its kind of weird for me but i'm sure there is an explanation for that.
The best way is to use interaction.options, so you'll need to add at least one option in your application command.
For example
// /test as your Application command
client.on('interactionCreate', async interaction => {
if (interaction.commandName === 'test') {
const message = interaction.options.data
console.log(message)
})
}
Slash commands have their own type of message. I don’t believe they have an id, delete button, edit button or lots of things most messages do. This means you will not be able to get the "message" from a slash command. From buttons however, they do emit INTERACTION_CREATE but has a little more info. I don’t remember for sure, but I think you can use something like interaction.components. I am not completely sure, but if you want, click a button and log the interaction into your console to see the unique button info like this
client.ws.on('INTERACTION_CREATE', async (interaction) => {
{
console.log(interaction) //will be long!
//…
})

How do I create a discord.js command in index.js [The bot's startup file] that deletes the message when a message that contains "hi" was sent

I've tried to create a command where the bot waits for a message that was sent until a message contained "thebotdeletesthismessage", the bot doesn't seem to do anything, the language I've used is node.js.
The bot doesn't really responds neither delete the message, I've tried to change the code a little bit, Still nothing, I need to know what I've done wrong here.
If you didn't understand something, Please let me know in the comments, And please don't close this topic, I really need help.
bot.on('message', message => {
if (message.content.starts("thebotdeletesthismessage")) {
message.delete
message.channel.send('No, dont send that.');
}
});
Change message.delete to message.delete().
First, there is no .starts() method, you probably want to use .startsWith(). Also, message.delete is not a property but a method, so you'll need to use parentheses after that.
The following code will delete every message that starts with "thebotdeletesthismessage" and sends a message instead:
bot.on('message', (message) => {
if (message.content.startsWith('thebotdeletesthismessage')) {
message.delete();
message.channel.send("No, don't send that.");
}
});

I made a bot blacklist but the bot gets stuck in a loop when anyone uses anything

I made a blacklist for the bot, it "works", it blocks the blacklisted user from using the bot but ended up causing a loop and the blacklisted message to get spammed, both when a blacklisted user uses it and when someone that isnt on the blacklist uses it
heres the code
client.on('message', async message => {
let blacklist = new Discord.MessageEmbed()
.setColor("#e31212")
.setDescription(
"ERROR: You are not allowed to use this bot | Reason: BLACKLISTED"
);
var blacklistids = ["402639792552017920", "711957885722296360"];
if (blacklistids.includes(message.author.id)) {
message.channel.send(blacklist).then(msg => {
msg.delete({ timeout: 3000 })
})
}
// Rest of the code
})
Your code seems correct - but by how you've described it, it would seem to me your code is wrapped in a loop somewhere else. Have you tried creating another client.on() with different conditions to see if those messages also get looped/spammed?.
This is something that can easily occur, especially if you are a beginner.
EDIT: you are however missing }); at the end of your client.on() declaration.
It looks like one of your IDs in the array might be the bot's ID... if this is the case, you might also want to have a if(message.author.bot) return after the client.on(...

discord.js bot replies to itself

I am currently coding my first discord bot, it can already play YouTube music.
if (message.content.includes("Good Job") ||
message.content.includes("good job")) {
message.channel.sendMessage("Good Job everyone :smirk:");
}
As you see, if someone types "good job" (this is just an example) then the bot will reply with "good job everyone :smirk:), but then the spam will begin: the bot reads his own message and replies to it.
How can I prevent the bot from answering itself?
Use this in the on message event:
if (message.author.bot) return;
for more info:
https://anidiotsguide.gitbooks.io/discord-js-bot-guide/coding-guides/a-basic-command-handler.html
The reason why your bot replies to yourself is because where you put:
if (message.content.includes("Good Job") ||
message.content.includes("good job"))
It is basically checking the chat if a piece of text includes the words "Good Job" or "good job". As your bot sends:
message.channel.sendMessage("Good Job everyone :smirk:");
as an answer, it creates a loop because that message includes the words "Good Job", basically running the code again and again and again.
Of course, the easiest way of fixing this is for you to change the answer it gives to not include the words Good Job, but there is better solution to make sure it doesn't happen for all of the commands you might make.
As #Jörmungandr said, under the message event include:
if (message.author.bot) return;
to make sure it doesn't happen. It essentially checks if the author of the message is a bot and if it is, it ignores it.
discord.js
// In message event
if(message.author.id === client.user.id) return;
// I would recommend a variable like this for splits on words
// const args = message.content.trim().split(/\s+/g);
// You could also .slice() off a prefix if you have one
if(/good job/i.test(message.content)) {
message.channel.send('Good job everyone :smirk:'); // sendMessage is deprecated, use send instead
}
You can simply just check if the user sending the message is a bot. For example:
if (!msg.author.bot) {
<Your code to execute if the user is not a bot.>
}
Hope this was helpful, thank you!
You may use this code which avoids doing anything if the author is a bot:
if(message.author.bot) return;
There are quite a lot of ways to solve this.
1: Stopping the Bot from Responding to ANY bot.
Right under your message callback (the client.once('message')), you can just add:
if (message.author.bot) return;
2: Stopping the Bot from Responding to itself
Once again, right under your message callback, you just add:
if (message.author.id == client.user.id) return;
Please note the client that I am referring to is your bot's client, created when you type
const client = new Discord.Client()
...or whatever your client is. It's basically the thing that you login to (ex: client.login(yourtokenhere))
Most tutorials will call this Client, client, or maybe even bot. The discord.js guide will call it a Client.

Categories

Resources