使用discord.py重复命令

使用discord.py重复命令,discord.py,Discord.py,我正在使用discord.py生成一个repeat命令,您在其中发送一个命令,它会重复您发送的消息。它可以工作,但唯一的问题是,如果我使用空格,例如“Hello I'm”,它只打印“Hello”。我该如何解决这个问题 这是我的密码: import discord import hypixel from discord.ext import commands bot = commands.Bot(command_prefix='>') @bot.event async def on_re

我正在使用discord.py生成一个repeat命令,您在其中发送一个命令,它会重复您发送的消息。它可以工作,但唯一的问题是,如果我使用空格,例如“Hello I'm”,它只打印“Hello”。我该如何解决这个问题

这是我的密码:

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')

首先,永远不要公开展示你的机器人令牌,这样任何人都可以为你的机器人编写代码,让它做任何人想要的事情

关于你的问题,, 如果使用
Hello I'm
调用该命令,它将只返回
Hello
。这是因为,在send函数中,您只接受一个参数

因此,如果发送
Hello I'm
它只接受传递给它的第一个参数,即
Hello
。如果再次调用该命令,但这次使用引号,
“Hello I'm”
例如,它将返回
Hello I'm

解决方案是将send函数更改为类似的函数,它将接受任意数量的参数,然后将它们连接在一起:

async def test(ctx, *args):
    channel = bot.get_channel(718088854250323991)
    await channel.send("{}".format(" ".join(args)))
它将连接传递给它的所有参数,然后发送该消息

如图所示

备选方案:仅使用关键字参数: 这也可以通过以下方式实现:

async def test(ctx, *, arg):
        channel = bot.get_channel(718088854250323991)
        await channel.send(arg)

同样,请参考位于

的官方文件,将代码更改为以下内容:

@bot.command()
async def send(ctx, *, message):
    channel = bot.get_channel(718088854250323991)
    await channel.send(message)
这允许您在同一消息中设置多个值。更好的方法是:

@bot.command()
async def send(ctx, *, message:str):
    channel = bot.get_channel(718088854250323991)
    await channel.send(message)

这将确保将消息值转换为字符串。始终是一个好习惯,因为您不知道是否可能输入错误并将其用作另一种数据类型。

只需这样写:

@bot.command()
异步命令(ctx,*,消息):
等待ctx.send(f“{message}”)

我很确定Discord已经更改了他/她的机器人令牌。我认为Discord会在网络上爬行并寻找机器人令牌。问题是只有一个论点。参数之间用空格隔开,所以如果你在那里写hello,它会认为只有一个参数是hello。通过执行异步def发送(ctx,*,消息)修复此问题。