How to get bot command values of another Discord bot - python-3.x

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)

Related

AIOGram. How to fix chat_member_handler?

I need my bot to do specific things when an user is joining end exiting a group. First, I write proof-of-concept code:
#dp.chat_member_handler()
async def user_joined_chat(update: types.ChatMemberUpdated):
print('Users changed')
But that does nothing. I added-deleted the test user to the test group many times, but nothing. Of course, I have made sure "privacy mode" is disabled and the bot is an administrator of the group before.
What's wrong? Do I use wrong handler?
You have to use other handler, handler that you are trying to use handles ChatMember status changes.
You have to use classic message_handler and handle content types like: NEW_CHAT_MEMBERS and LEFT_CHAT_MEMBER you can find such types here
so there is working code:
#dp.message_handler(content_types=[types.ContentType.NEW_CHAT_MEMBERS, types.ContentType.LEFT_CHAT_MEMBER])
async def user_joined_chat(message: types.Message):
print('Users changed')

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 )

How to add reaction to a specific message using the ID? (discord.py)

I've been trying for hours a command that react to a message using the ID.
If someone writes !react (the message ID) the bot reacts to the message.
Can someone help me? I have no clue how to do this.
Use a converter to get the discord.Message instance of the message:
#client.command()
async def react(ctx, message: discord.Message):
...
Then use Message.add_reaction to add a reaction to it, which I'm sure you can figure out by yourself.
Keep in mind that in case the message ID is invalid, can't be found, or is not in the same channel as where the command gets called, the converter will fail & throw you an exception. If you pass in the message's URL instead of the ID, Discord will be able to figure out what channel the message was sent in so you can call the command from wherever you want.
You can get a message's URL by selecting Copy Message Link, which might be better for your users as getting the id requires Developer Mode to be on, which most people don't have enabled. The MessageConverter mentioned in the beginning can parse both id's and URL's so you don't have to worry about that part.

How to mention the author of a message in another message

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}`);

message filter for bot's messages undefined node.js

I am having a bot set up the beginnings of a game. A human inputs the command of /startbrawl to initiate the setup of the game (creating the deck objects). But the two players need to be identified first. I have a message sent from another command that says "Player A is #[username A]. Player B is #[username B]." in the channel this game is happening in. I want the bot from this new command to look at the first message sent in the channel, which is always the "Player A is etc..." message (and is always sent by the bot) and pull both usernames from it in order to specify for this new command who is player A and who is player B. The code I have most recently (after trying multiple things) is this:
if (userInput.startsWith("!startbrawl") === true) {
message.channel.fetchMessages().then(messages => {
const botMessages = messages.filter(message => message.author.bot);
console.log(botMessages.mentions.members.first()) //this will be Player A. I'd repeat same for Player B with .last instead.
}
}
This gives me an error:
(node:15368) UnhandledPromiseRejectionWarning: TypeError: Cannot read
property 'first' of undefined.
I have made the last line be console.log(botMessages) to get all the info about the messages the filter finds. But trying to extract only part of it gives issues about not being defined, or just a result of undefined with no errors. Either way, something isn't working how I think I need it to.
The only other thing I've debated trying is exporting variables from the command prior to this new command. Player A and Player B are defined in the command used to make the channel that this new command is then used in. However, I've never had luck with exporting variables when I've used it in other instances. I use a command handler, so I'm not sure if that affects how exporting variables works... Which method would work best to set up the card game? I'm a novice in general just figuring things out as I go, so some advice (beyond take a course, look up basics, etc) is greatly appreciated. I've taken an online course for javascript and I work best figuring things out first hand.
Thanks for the help in advance!
As botMessages is a collection you wanna get a Message object out of it by doing
botMessages.first()
So try logging something like
botMessages.first().mentions.members.first()

Resources