How to mention the author of a message in another message - node.js

I am using repl.it to develop a bot. I am trying to make a command that makes the bot behave like this:
Someone: !slap #someoneelse
Bot: #Someone slapped #someoneelse
How can I get the bot to mention #someone without using ID? Multiple people will use the command and I can't just use ID since it will only work with one person. I haven't found anything that helped me, and the documentation was no help either. Hopefully, I can get help! Thank you.

Users and members have a .toString() method that is automatically called every time they are concatenated with a string: that means that if you type "Hey " + message.author you will get "Hey #author"
That's how I would do the command:
// First store the mentioned user (it will be undefiend if there's none, check that)
let mentionedUser = message.mentions.users.first();
// Reply by directly putting the User objects in the string:
message.channel.send(`${message.author} slapped ${mentionedUser}`);

Related

How to get bot command values of another Discord bot

I'm trying to have my bot keep track of items that are being spawned by another bot (Mee6).
The following code gives me a None output.
#client.event
async def on_message(message:discord.Message):
if message.author.bot:
print(message.content)
The command the other bot is responding to is:
/spawn-item member={member} item={item} amount={amount}
I would like to retrieve these values.
Any help would be welcome!
Your code doesn't work, because you want to get an interaction, not a message.
Unfortunately, there is no way to get the interaction of another client. At most you can have a MessageInteraction, which gives you the name of the command used.
But in your case you can make it work since MEE6 still accepts the old command format. If you use the prefix of MEE6 instead of the / command, your code should work (with a bit of reworking: you would need to look at the message right before MEE6's response, or look if a message starts with the MEE6's prefix)

How can I save a Message in Discord with Python, in a simple variable

Hello Stackoverflow Community,
I wanted to write a Discord-Bot in Python, which should solve simple Mathquestions, like: "Whats 55+40?". The Questions are asked from another bot and the structure of the Question is everytime the same. I wrote a Calculator and it works and i just need the input from a private message(not a server). I do not need to show any code, because I dont have some and I need to start at the described situation to save the messages in a variable, so my Question is:
How can I import private messages from Discord, to save them in a normal variable, like x?
I use Sublime Text
Thanks for answers!
Looking at the comments of the question it is clear that you don't have any code for a bot yet. Look at this quickstart guide to set up a bot. Once set up, all messages typed to the bot (also DM messages) will trigger the on_message event. You can then save the contents of the message into a variable like so
#client.event
async def on_message(message):
messageContent = message.content
i assume what you mean is waiting for a user to reply to a certain message from the bot, you can get your bot to private message someone by doing something like this:
user = ctx.author
await user.send("Hello, world!")
now, for your bot waiting for a reply for your user, in your code, you will need a check just to make sure only the bot is waiting for a message from the specific person (even in dms) by creating a check function:
def check(m):
return m.author.id == ctx.author.id
then after using user.send to send a message, or an embed, add
message = await bot.wait_for('message', check=check, timeout=120)
which means the bot waits until the user sends their answer (waits 120 seconds in this example)
then, to get the content of the message you just want to do make a variable called content or answer and set it to message.content ( content = message.content )

Preventing a discord bot from executing a command in a user prompt

I need some help/suggestion to prevent a discord bot from executing a command in a user prompt.
My bot currently has a feature that prompts the user a question, and it'll add whatever the user's answer is to the database. The problem that I'm trying to solve is, currently the user's able to enter a bot command as an answer, and the bot would both execute that command and take that as an answer to add to the database.
A very quick example to show how problematic this can get:
User: ?question
Bot: Cats or Dogs?
User: ?question
Bot: "?question" have been added to the database
Bot: Cats or Dogs?
I don't have a problem with the bot adding the command to the database, because that's what the user entered (it might be relevant for the user to enter a bot command there), but I don't want the bot to execute that command.
Right now I have 2 vague ideas to solve this (I don't know whether or not any of this will be valid):
I need to turn the user's answer into an "answer" type variable where the bot can't use it to search for commands, but can still use it to upload to the database and fetch from it and display in a list of answers. Although I don't know if this can be executed before the bot starts to search for the command.
I need to somehow change how this system of question & answer works.
Note: Currently my bot detects a command by slicing the first bit of the user's message .slice(config.prefix.length)
Any help or suggestion will be much appreciated.
Thanks in advance!
Here's some answers to your question:
The bot takes in a command as an answer.
If you ever change your mind, you can disallow this by simply allowing only a few selected responses ("cat" or "dog") or disallowing commands that start with a ?.
The command is executed if a command was used as an answer.
This is because you check if the message is a command down the line, somewhere in your code. Not only does the bot recognize the user's message as a valid answer, but it also recognizes that it fulfills the prefix you set and thus it runs the command.If you have a flag that indicates that the user's next response is an answer to the Q&A question, then you can check if that flag is active before executing the command. For example:
// Assume that the user's ID is in this array after they
// requested to answer a Q&A question.
var usersAwaitingResponse = [];
...
client.on("message", function(message) {
if (usersAwaitingResponse.includes(message.author.id)) {
// take in the answer and then end the function by calling "return;"
} else {
// check if the message was a command and act accordingly
}
});
Prefix Checking
Lastly, I recommend that you check if a message is a command by using .startsWith(config.prefix) on the message text. If you want the user to only input allowed characters after the prefix, then you can use a regular expression. Either method saves time and memory rather than slicing the string. An example of those can be seen below:
// using "startsWith"
function checkIfCommand(message) {
return message.content.startsWith(config.prefix);
}
// using a regular expression
function checkIfCommand(message) {
/**
* If all the matches are fulfilled, the test passes:
*
* ^ - The match should be at the beginning of the string.
* ${config.prefix} - The prefix (interpolated into the string).
* [A-Z0-9]+ - match as much characters that are A to Z or 0 to 9
* \s? - Match a whitespace character (if there is one)
* "gi" - global, case insensitive
**/
return new RegExp(`^${config.prefix}[A-Z]+\s?`, "gi").test(message.content);
}

How would I make a command that changes a user's nickname when used? (discord.py-rewrite)

I'm trying to create a command for my bot that allows users to change their nickname. I've tried the answer stated here but I get a 'client is not defined' error and when I redefine in within the file itself, it returns a 'client has no 'change_nickname' member' error. The intended usage is supposed to be something like t!callme Jack and the user that used the t!callme command would have their nickname set to 'Jack'. Does anyone know how I could go about this?
Now if you wanted to change the author of the messages nickname, then all you have to do is add the following:
member = ctx.message.author
await member.edit(nick="whatever your heart desires")
member is set to the author of the message, and then we tell the bot that the author of the message (or 'member') will change their nickname to 'whatever your heart desires'.
Make sure that the bot has permission to Manage Nicknames, or else this command will not work and it will give you a '403 FORBIDDEN' error.
It should look something similar like what I provided below:
#bot.command()
async def callme(ctx):
member = ctx.message.author
await member.edit(nick='Nickname wanted')
#This line is used just to keep chat nice and tidy :)
await ctx.message.delete()

Using user message in retryPrompt

In a given moment, my bot asks for a number.
builder.Prompts.number(session, "Cool. What's the number?", {
retryPrompt: "I couldn't understand $%##. Could you type it again?"
});
I need the message to contain the user response instead of $%##. Is there a way to do it? I tried to use session.message.text and results.response, but these are still from the outer function because of closure.
For anyone looking for this answer: currently, there is no way to change the message inside the Prompt dialog.
I looked up the code for BotFramework for nodeThe message you send as retryPrompt is as is, without replacements.

Resources