How to get information out of a list - python-3.x

So iam coding an discord bot and i want to get information out of an list that looks like this and this output is in "information = await guild.invites()"
[<Invite code='GZqCe8EF' guild=<Guild id=847546806937059338 name='Testing' shard_id=None chunked=False member_count=2> online=None members=None>, <Invite code='jQ2HeQfx' guild=<Guild id=847546806937059338 name='Testing' shard_id=None chunked=False member_count=2> online=None members=None>]
is it possible to get single things out like guild id or maybe 2 things like name and invite code and is it possible that u can output every line from it like the first invite code and the second one?

To output every invite code, you can use:
for invite in information:
print(invite.code) #invite.code returns the invite code, do whatever you want to do with it
# you can also add invite.guild.name to for the guild name etc
To output only from one invite:
print(information[0].code)
References
invite object with every possible paramenter after .

Yes it is possible, the invites() method returns list of Invite objects. To check what attributes does this object have look here.
to access an attribute you have to do this: InviteObject.{name of attribute}. Example:
for invite in information:
print(invite.guild.id)
It works the same for any discordpy object:
Look for the method that you are using in discordpy docs (In your case invites())
See what does it return (In your case list of Invite objects)
Search for the object (In your case Invite)

Related

How to handle replies in with Pyrogram when using ForceReply?

I am building a telegram bot where I am attempting to get the user to fill in detail about an event and store them in a dictionary which is itself in a list.
However I want it be link a conversation. I want it to look like:
user: /create
bot-reply: What would you like to call it?
user-reply: Chris' birth day
bot-reply: When is it?
user-reply: 08/11/2021
bot-reply: Event Chris birth day on 08/11/2021 has been saved!
To achieve this I plan to use ForceReply which states in the documentation
This can be extremely useful if you want to create user-friendly step-by-step interfaces without having to sacrifice privacy mode.
The problem is the documentation does not seem to explain how to handle responses.
Currently my code looks like this:
#app.on_message(filters.command('create'))
async def create_countdown(client, message):
global countdowns
countdown = {
'countdown_id': str(uuid4())[:8],
'countdown_owner_id': message.from_user.id,
'countdown_onwner_username': message.from_user.username,
}
try:
await message.reply('What do you want to name the countdown?',
reply_markup=ForceReply()
)
except FloodWait as e:
await asyncio.sleep(e.x)
Looking through the form I have found options like this:
python telegram bot ForceReply callback
which are exactly what I am looking for but they are using different libraries like python-telegram-bot which permit them to use ConversationHandler. It seems to not be part of pyrogram
How to I create user-friendly step-by-step interfaces with pyrogram?
Pyrogram doesn't have a ConversationHandler.
You could use a dict with your users' ID as the key and the state they're in as the value, then you can use that dictionary of states as your reference to know where your User is in the conversation.
Dan: (Pyrogram creator)
A conversation-like feature is not available yet in the lib. One way to do that is saving states into a dictionary using user IDs as keys. Check the dictionary before taking actions so that you know in which step your users are and update it once they successfully go past one action
https://t.me/pyrogramchat/213488

Telethon utils.resolve_invite_link(link) is returning wrong Chat/Channel ID

I'm trying to generate the channel ID of my private channel in Telegram.
I use the following:
link = input("Please provide channel invite link: ")
print(utils.resolve_invite_link(link))
My output looks like the following (I've scrambled the numbers):
(0, 0123456789, 1234567891234567891)
When I view the private channel in the web browser, I get the channel ID as https://web.telegram.org/z/#-9876543210
So the channel ID should be -1009876543210, which I confirmed with IDBot.
Why isn't 9876543210 appearing when I call variable link within utils.resolve_invite_link()? That's the value I expected to see, not 0123456789.
utils.resolve_invite_link(link) no longer works with the new links.
Old links used to pack the channel/group ID inside the links themselves but this is no longer the case. The function will possibly be removed as well in future updates of the library https://github.com/LonamiWebs/Telethon/issues/1723
The most reliable way now is to use CheckChatInviteRequest https://tl.telethon.dev/methods/messages/check_chat_invite.html

Reacted user as channel name with Discord.js

I'm building a ticket system that whenever a user react's with the emoji, a channel get's created with him in it and the team that deals with the tickets. My problem is that when I want to display the user's name in the channel through the following code await message.guild.channels.create(``🎫│${reactor}``, {type: 'text',} and reactor is defined by const reactor = reaction.message.guild.members.cache.get(user.id); I get the user's ID in the channel name. My question is if there's a way to name the channel 🎫│User instead of 🎫│ID (with ID I mean the user's Discord ID, not the actual text 'ID').
There's quite a simple solution to your issue, being that you're attempting to use a GuildMember object, instead of a User object.
At the time that I'm writing this (And it will probably stay like this forever), you're not able to get the username of a GuildMember, but would first of all have to convert it into a User object.
This could be very easily done with the following code:
const member = reaction.guild.member(user.id)
const name = member.user.username
message.guild.channels.create(`${name}s ticket`, {
type: 'text'
})
I'd also like to mention that you're not required to add message after reaction when you're trying to get the guild object! You can simply use the reaction variable you've created in your callback and use it to get the guild object using reaction.guild!

How to get the bot to recognize when a role was mentioned, and then respond back?

I would like the bot to recognize when the #Community Manager, #Admin, #Moderator roles are tagged, either single, or multiple roles tagged in same message, then send a message to the channel mentioning the user name.
I can get the bot to recognize when it's been tagged using this code:
if client.user.mentioned_in(message) and message.mention_everyone is False:
await message.delete()
I cannot for the life of me figure out how to see if other roles were tagged.
I've tried
if message.role_mentions.name('Admin'):
#do stuff
but get this error:
AttributeError: 'list' object has no attribute 'name'
message.role_mentions gives back a list of Role.
And then you can fetch roles from the guild using message.guild.get_role(id) in order to compare against the list of roles you gotten from the message.
Should result in something along the lines of this:
# Create a list of roles to check against
rolesToCheckAgainst [
# Fetches the role from the guild which the message was sent from
message.guild.get_role("The ID of the role"),
message.guild.get_role("The ID of the second role")
#etc...
]
# 'rolesWerePinged' will be true if any of the roles were pinged
rolesWerePinged = any(item in rolesToCheckAgainst for item in message.role_mentions)
if rolesWerePinged:
# Do something, some of the roles was pinged.
Also, I used any() to check if any of the roles mentioned contained any of the roles that needed to be check against. You can use a double-loop instead if you need different actions to be done depending on the type of roles mentioned.

Getting arguments/parameters values from api.ai

I'm now stuck on the problem of getting user input (what user says) in my index.js. For example, the user says: please tell me if {animals} can live between temperature {x} to {y}. I want to get exact value (in string) for what animals, x and y so that I can check if it is possible in my own server. I am wondering how to do that since the entities need to map to some exact key values if I annotate these three parameters to some entities category.
The methods for ApiAiApp is very limited: https://developers.google.com/actions/reference/nodejs/ApiAiApp
And from my perspective, none of the listed methods work in this case.
Please help!
Generally API.AI entities are for some set of known values, rather than listening for any value and validating in the webhook. First, I'd identify the kinds of entities you expect to validate against. For the temperatures (x and y), I'd use API.AI's system entities. Calling getArgument() for those parameters (as explained in the previous answer) should return the exact number value.
For the animals, I'd use API.AI's developer entities. You can upload them in the Entity console using JSON or CSV. You can enable API.AI's automated expansion to allow the user to speak animals which you don't support, and then getArgument() in webhook the webhook will return the new value recognized by API.AI. You can use this to validate and respond with an appropriate message. For each animal, you can also specify synonymous names and when any of these are spoken, and getArgument() will return the canonical entity value for that animal.
Extra tip, if you expect the user might speak more than one animal, make sure to check the Is List box in the parameter section of the API.AI intent.
If "animals", "x", and "y" are defined as parameters in your Intent in API.AI, then you can use the getArgument() method defined in ApiAiApp.
So the following should work:
function tempCheck( app ){
var animals = app.getArgument('animals');
var x = app.getArgument('x');
var y = app.getArgument('y');
// Check the range and then use something like app.tell() to give a response.
}

Resources