我正在使用 discord.py 制作一个重复命令,您在其中发送命令并重复您发送的消息。它有效,但唯一的问题是如果我使用空格,例如“你好,我是”打印出“你好”。我该如何解决?
这是我的代码:
import discord
import hypixel
from discord.ext import commands
bot = commands.Bot(command_prefix='>')
@bot.event
async def on_ready():
print("Ready to use!")
@bot.command()
async def ping(ctx):
await ctx.send('pong')
@bot.command()
async def send(ctx, message):
channel = bot.get_channel(718088854250323991)
await channel.send(message)
bot.run('Token')
Run Code Online (Sandbox Code Playgroud)
小智 5
首先,永远不要公开展示你的机器人令牌,这样任何人都可以为你的机器人编写代码并让它做任何他想做的事。
至于你的问题,如果你用 调用命令Hello I'm,它只会返回Hello。这是因为,在您的发送函数中,您只接受一个参数。
因此,如果你发送Hello I'm它,它只接受传递给它的第一个参数,即Hello. 例如,如果您再次调用该命令,但这次使用引号,"Hello I'm"它将返回Hello I'm.
对此的解决方案是将您的发送函数更改为这样的内容,这将采用任意数量的参数,然后将它们连接在一起:
async def test(ctx, *args):
channel = bot.get_channel(718088854250323991)
await channel.send("{}".format(" ".join(args)))
Run Code Online (Sandbox Code Playgroud)
这将加入传递给它的所有参数,然后发送该消息。
如此处所示官方文档
替代方法:使用仅关键字参数:这也可以通过以下方式完成:
async def test(ctx, *, arg):
channel = bot.get_channel(718088854250323991)
await channel.send(arg)
Run Code Online (Sandbox Code Playgroud)
再次参考Keyword-only arguments 中的官方文档
| 归档时间: |
|
| 查看次数: |
3024 次 |
| 最近记录: |