AIOGram. How to fix chat_member_handler? - python-3.x

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')

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 do I make a on_mentioned event in Nextcord python?

I want to make my Discord bot tell a member not to ping the owner.
I'm not too sure if on_mentioned is an actual event. If anybody can help,
that'd be a huge help.
Here's what I'm working with at the moment:
#bot.event
async def on_mentioned(ctx):
author = ctx.author.mention
await ctx.send(f'{author} please do not ping that member. They are either AFK or do not want to be pinged.')
You can check for mentions in on_message.
#bot.event
async def on_message(message):
for mention in message.mentions:
# do something
message.mentions will be a list of users mentioned, you can check if it contains any mentions or not.
Note about overriding on_message -- the bot will not process commands if you override this without running bot.process_commands(). You can alternatively, use a listener. See https://docs.nextcord.dev/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working.
I'm not too sure if on_mentioned is an actual event
That's what docs are for. You can check the Event Reference or search for it.

Telethon Python channel statement

I'm trying to create a user bot that will forward a message from specific channel to a group. I succesfully managed forwarding all messages to the certain group (but that's quite annoying) and want to create statement when on specific group message will be received, then forward it to the group.
#client.on(events.NewMessage(pattern='channelID'))
async def forward(event):
await event.forward_to(-groupID)
However the pattern "statement" will only check words that are sent to me. Is there possibility to create statement for channel ID?
#client.on(events.NewMessage(channelID))
async def forward(event):
await event.forward_to(destinationGroupID)
Solution here.

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.

What is the proper way to return a JSON object to Alexa Smart Home or end AWS Lambda in NodeJS?

I have seen three ways to return a JSON object or end a Lambda function. My trigger is Smart Home Alexa.
I am using now is context.succeed(response_JSON);This one works for me. Even if this instructions is inside a nested function. The whole Lambda ends and return the response_JSON to Smart Home Alexa.
I have seen in other blogs that say callback(response_error,response_JSON). This one did not work for me. It did not return anything to Smart Home.
Others just uses the return response_JSON. I have not used this one.
I am using now is context.succeed(response_JSON);This one works for me. Even if this instructions is inside a nested function. The whole Lambda ends and return the response_JSON to Smart Home Alexa.
context.succeed()/fail() causes the Lambda function to terminate immediately. However, I have not seen this documented in the context object docs, so it may get deprecated in later Node versions (?).
I have seen in other blogs that say callback(response_error,response_JSON). This one did not work for me. It did not return anything to Smart Home.
This one probably doesn't work for you because by default Node.js waits for the event loop to be empty before executing the callback statement. This may be due to open network/database connection. As per the doc, set the context.callbackWaitsForEmptyEventLoop variable to false to send the response right away.
Others just uses the return response_JSON. I have not used this one.
This should be used with async handlers. Read more about async and non-async handlers here: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html

Resources