I am making a command in a bot to create a profile for a user. It is working fine, but I would like the description of the "name" parameter to say "What would you like to be called?".
Here is the code I currently have:
import discord
from discord import app_commands
#tree.command(name="makeprofile", description="Make your own profile!", guild=discord.Object(id=000000000000))
async def make_profile(interaction, preferred_name: str, pronouns: str):
db.insert({'id': interaction.user.id, 'name': preferred_name, 'pronouns': pronouns})
From the documentation:
#discord.app_commands.describe(**parameters)
Describes the given parameters by their name using the key of the keyword argument as the name.
So in your case:
#app_commands.describe(preferred_name = "What would you like to be called?")
Related
As the title says. I need my telegram bot to take user input, and use that to change some values on another function from another file. I already got the file to be successfully run from the bot, but I can't figure out how to change values first. I am using Python-Telegram-bot.
here is the code I need to edit that is in a separate file (call.py)
call = client.calls.create(
machine_detection='Enable',
url='https://ngrok.io/main',
to='',
from_=''
)
I need to edit the "to" and "from" field(s) in this code above.
The code I use to run this from my bot is as follows:
def update(update, context):
update.message.reply_text('Enter number :\n'
'e.g. 18004585478\n')
update.message.reply_text('Calling...')
exec(open("call.py").read())
I am pretty new to all this so I know the code is not good at all. I have read that I should be using ConversationHandler or CommandHandler but I honestly am not sure how to implement it.
I edited the code based on what Alexey suggested and now am stuck on a similar issue.
def update(update, context):
update.message.reply_text('Enter number:\n'
'e.g. 18004585478\n'
'Number Must begin with 1')
from_number = update.message.text
update.message.reply_text('Enter number:\n'
'e.g. 18004585478\n'
'Number Must begin with 1')
to_number = update.message.text
update.message.reply_text('Calling...')
call_state = call.make_call(to_number, from_number)
The Telegram bot just runs all the code at once, it doesn't stop and wait for any input from the number fields. How do I go about implementing MessageHandler to make the bot stop and accept input to pass along to call_state, then execute call_state at the end?
You don't need to change the code, you need to use arguments to pass the data you wanted to.
In call.py you can make a funciton
def make_call(to_number, from_number):
call = client.calls.create(
machine_detection='Enable',
url='https://ngrok.io/main',
to=to_number,
from=from_number,
)
return call
In your update function just use the function by giving it the necessary values
import call
def update(update, context):
update.message.reply_text('Enter number :\n'
'e.g. 18004585478\n')
update.message.reply_text('Calling...')
call_state = call.make_call(to_number='0123456789', from_number='9876543210')
# use call_state ...
What Alexey stated ended up working with very slight modifications.
I took what Alexey posted and deleted the numbers and turned them into a variable I could edit from my other script.
def make_call(to_number, from_number):
call = client.calls.create(
machine_detection='Enable',
url='https:snip/main',
to=to_number,
from_=from_number
)
print(call.sid)
Then in the other file I defined the variables and executed them by importing the file I needed to edit by using user_data[FROM] = update.message.text and user_data[TO] = update.message.text.
then calling the funciton.
call_state = call.make_call({user_data[TO]}, {user_data[FROM]})
Dont forget to add user_data = {} at the top of your code.
I got a little problem that make stuck me at code small script that collect data from telegram chats. For first i will show my code and config file:
Config.ini:
[account]
api_id = xxxx
api_hash = xxxx
[parser]
channels_to_parse = [-xxxx,-xxxx]
Run.py
import configparser
import asyncio
import time
from telethon import events
from telethon import TelegramClient
from telethon.tl import functions, types
from datetime import datetime
#load config file
config = configparser.ConfigParser()
config.read('config.ini', encoding="utf-8")
#telethon init
client = TelegramClient('sess', config.get("account", 'api_id'), config.get("account", 'api_hash'))
client.start()
#main cycle
#client.on(events.NewMessage(chats=config.get('parser' , 'channels_to_parse')))
async def main(event):
#some code...
client.run_until_disconnected()
The main problem goes from string that contains arguments for telethon that points chats IDs from which i collecting data:
ValueError: Cannot find any entity corresponding to "[-xxxx]"
When i passing arguments manually, without configparser:
#client.on(events.NewMessage(chats = [-xxxx, -xxxx]))
Everything works well. So i think that issue related to configparser or configparser parameters. I checked configparser docs and didn't find anything that can help me.
I already tried to use channels name instead IDs. Maybe who's can explain me what i do wrong.
I can't check this at the moment, but I have an idea that configparser returns the str type and you need a list. But I could be wrong(
UPDATE
I checked it out and I was right!
config.ini:
[parser]
channels_to_parse = -xxxx -xxxx
file.py:
parser = config.get('parser', 'channels_to_parse') # type str
chats = [int(i) for i in parser.split()] # type list
#client.on(events.NewMessage(chats=chats))
I want to remove the formatting from text in discord.py - adding \ before *'s, ```'s etc. I have not yet been able to come up with a perfect solution.
Can anyone tell me what I could use?
I use discord.py and python 3
If this is in a command you can use commands.clean_content
#bot.command()
async def test(ctx, arg: commands.clean_content(fix_channel_mentions=False, use_nicknames=True, escape_markdown=True, remove_markdown=False)):
await ctx.send(arg)
All params are kwarg only and optional, for more info see the docs
If you want to escape markdown for some other text (e.g. from an API), you can use utils.escape_markdown
import discord
text = "Hello my name is **Wasi**"
print(discord.utils.escape_markdown(text))
# 'Hello my name is \*\*Wasi\*\*'
There is also utils.escape_mentions for removing mentions
Discord.py provides a utility function: discord.utils.escape_markdown
This converts:
#```python
#print("hello")
#```
#**bold** *italics*
into
#\`\`\`python
#print("hello")
#\`\`\`
#\*\*bold\*\* \*italics\*
Yeah, so I got the user ID of the Users, but I'm unable to find code to get the user name, like I tried everything like client.fetch_user(payload.user_id) and client.get_user(user_id) but it won't work.
Here is the code:
#client.command(pass_context = True)
async def test(ctx,id):
print(getname(id))
def getname(a):
return client.get_user(a).name
thanks.
If you're trying to have have it in a different function then you can do:
async def getname(ctx):
user = await ctx.author.guild.fetch_member(id)
return user
If you're trying to get the name specifically try adding .name behind ctx.author.guild.fetch_member(id)
Maybe this is the answer you're looking for?
I am accessing the class from the code api_service.py, which can be found here. When I call the first function, I have no problem, because no variables are passed:
from api_service import ApiService
import json
def main():
api_key = *removed*
access_token = *removed*
calling = ApiService(api_key,access_token)
survey_list = calling.get_survey_list()
But when I use the same type of routine as above to call a function from ApiService that requires a variable, I'm told that I should pass an object.
survey_details = calling.get_survey_details("1234")
survey_details = json.loads(json.dumps(survey_details))
print survey_details
The specific error message:
{u'status': 3, u'errmsg': u"Value '1234' for field '_data' is not of type object"}
Details for the get_survey_details aspect of the SurveyMonkey API are here, although I think a python-guru can solve this without knowing about the API.
This is a javascript/json object:
{field:'value'}
You have passed a string which, doesn't count as an "object" for these purposes.
Note that the error message is being generated by the service you are accessing. This question would be better directed to the creator of the service.