Clarification needed on app_commands.Transformer
#9311
-
Very much a beginner here, so sorry if this comes off ignorant. Here's an excerpt from discord.py examples: class Point(NamedTuple):
x: int
y: int
class PointTransformer(app_commands.Transformer):
async def transform(self, interaction: discord.Interaction, value: str) -> Point:
(x, _, y) = value.partition(',')
return Point(x=int(x.strip()), y=int(y.strip()))
@client.tree.command()
async def graph(
interaction: discord.Interaction,
point: app_commands.Transform[Point, PointTransformer],
):
await interaction.response.send_message(str(point)) What i have so far: class Urls():
urls:str
def __init__(self, urls):
self.urls:list = urls.split()
self.filter_imgurls()
#Sets self.img inside __init__. Also makes alterations to self.urls:list
def filter_imgurls(self) -> None:
...
#Returns a string derived from self.urls:list
def give_names(self, lntrn:str, sprtr:str) -> str:
...
class UrlsTransformer(app_commands.Transformer):
async def transform(self, interaction: discord.Interaction, value: str) -> Urls:
return await Urls(value)
def run():
...
@bot.tree.command()
async def test (
interaction:discord.Interaction,
urls:app_commands.Transform[Urls, UrlsTransformer]
):
await interaction.response.send_message(f"{urls.img}", ephemeral=True)
...
if __name__ == "__main__":
run() By itself, the class I want to "transform" command input to works just fine. But, so far, my attempts to hook it up to a Now, what I immediately notice is that What am i doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
You should provide the actual message of your error You first annotate urls as string in the class body but then redefine the type as list in the body of |
Beta Was this translation helpful? Give feedback.
-
I've heard of it, but haven't read it yet. Now it's moved closer to the top on my list. Thank you.
Ok. I will work on that. It seems to me that it just takes a moderate readjustment of perception, but if the pay off = making other people understand my code better and/or hide from them that I'm a gigantic amateur - I'm willing to try.
Well, it seemed at the time that removing items from the list without def remove_b(incoming:list, discarded:list) -> None:
for i, el in enumerate(incoming):
if el == 'b':
beees = incoming.pop(i)
discarded.append(beees)
my_list:list[str] = ['a', 'b', 'a', 'a', 'b', 'a', 'b', 'a', 'b']
junk:list = []
remove_b(my_list, junk)
print(my_list) #['a', 'a', 'a', 'a', 'a']
print(junk) #['b', 'b', 'b', 'b'] I also wanted to do multiple "checks" and append discarded items to another list. Moreover, for some reason I didn't like my "main"(?) calling multiple "helpers"(?) consecutively... seemed redundant (imagine I had So, when I used the approach above, I noticed that pop causes my logger to "skip a beat" and if, for example, I had 4 items in a list it showed me only 3 operations from the loop (even though it got through all of them). This was probably unrelated to the aforementioned approach, but rather it was some other logical error on my part. I'm not sure what it was, but I couldn't find it at the time. Then I fooled around some more and discovered generators. Experimenting with them I figured that they did essentially the same thing while NOT making my logger confused and adding less "bulk" to the code (I guess). what I could have done is something like this (probably): def is_b(element:str):
return element == 'b'
def is_c(element:str):
return element == 'c'
my_list:list[str] = ['a', 'b', 'c', 'a', 'b', 'a', 'b', 'c', 'b']
my_other_list = [x for x in my_list if not any([is_b(x), is_c(x)])]
print(my_other_list ) # ['a', 'a', 'a'] But It doesn't Also, I had a plan to return a And now you tell me that using |
Beta Was this translation helpful? Give feedback.
Yeah there is room for improvement i will only give a shallow code review i think others may chime in with other improvements
interaction.client.session
see https://github.com/Rapptz/discord.py/blob/master/examples/advanced_startup.py#L87 for an example on how to do something like that (ignore the asyncpg st…