以空格为单个字符串的参数来自不和谐输入

发布于 2025-01-26 16:07:14 字数 951 浏览 4 评论 0原文

晚上好。在正常情况下,在函数中使用空格的争论只需要您用引号封装参数。这适用于大多数用途,但是这是因为我使用Discord接口来处理输入而有所不同。

我正在根据用户输入来获取Wikipedia文章:

import wikipedia
...

@commands.command()
async def wiki(self, ctx, wiki):  # How would I treat the wiki parameter as a single argument?
        page = wikipedia.page(wiki, auto_suggest=False, redirect=True, preload=False)  # Fetch Wikipedia page.
        embed = discord.Embed(title=page.title, url=page.url, description=page.summary)  # Create a fancy embed.
        await ctx.send(embed=embed)  # Send embed in current channel.

这不是Discord.py或Wikipedia API中的问题,我的问题更广泛。如果用户尝试输入传输安全层,则仅查询第一个单词(传输),因为所有空间都被视为单独的参数的单独参数。 您将如何将输入视为单个参数?我尝试在代码中格式化和替换空格,但是到目前为止,我唯一的解决方案是手动引用不和谐中的输入。

一如既往地感谢您抽出宝贵的时间!任何建议都将不胜感激。

我的 ://i.sstatic.net/wui14.png“ alt =”第二个示例显示了我当前的解决方案。

Good evening. In normal cases, taking an argument with spaces in a function only requires you to encapsulate the argument with quotes. This works for most uses, but this one differs since I'm using the Discord interface to process inputs.

I'm fetching a Wikipedia article based on user input:

import wikipedia
...

@commands.command()
async def wiki(self, ctx, wiki):  # How would I treat the wiki parameter as a single argument?
        page = wikipedia.page(wiki, auto_suggest=False, redirect=True, preload=False)  # Fetch Wikipedia page.
        embed = discord.Embed(title=page.title, url=page.url, description=page.summary)  # Create a fancy embed.
        await ctx.send(embed=embed)  # Send embed in current channel.

This is not an issue that lies within Discord.py or the Wikipedia API, my question is broader. If a user tries to input Transport Security Layer, only the first word (Transport) will be queried since all spaces are treated as separate arguments for separate parameters. How would you treat the input as a single argument? I have tried formatting and replacing spaces in the code, but as of now, my only solution is to manually quote the input in Discord.

Thank you as always for taking your time! Any suggestions at all are appreciated.

Example of my problem. Only the word 'Transport' is processed.

Second example shows my current solution. Encapsulating the input directly works, but it's tedious and prone to errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百思不得你姐 2025-02-02 16:07:14

有一个“消耗式恢复”参数选项。您可以阅读更多在这里

@commands.command()
async def wiki(self, ctx, *, wiki):

There's a "consume rest" argument option. You can read more here.

@commands.command()
async def wiki(self, ctx, *, wiki):
能怎样 2025-02-02 16:07:14

另一种方法是从ctx.message.context获取整个字符串,然后自己操作。

@commands.command()
async def wiki(self, ctx: commands.Context):
    wiki = ctx[6:] 

Another way to do this is to get the entire string from ctx.message.context and just manipulate it yourself.

@commands.command()
async def wiki(self, ctx: commands.Context):
    wiki = ctx[6:] 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文