使用discord.py重复命令

2024-04-19 07:13:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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')

Tags: pyimport命令sendmessagehelloasyncdef
3条回答

就这样写吧:

@bot.command()
async def yourcommand (ctx,*,message):
    await ctx.send(f"{message}")

啊,这很容易

@client.command()
#* for infinite args
async def echo(ctx,*,args:str):# it will turn every thing into a str like echo hi , hi will be a str automatically 
    await ctx.send(args)

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

关于你的问题,, 如果使用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)))

它将连接传递给它的所有参数,然后发送该消息

如图所示Official Docs

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

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

再次提及Keyword-only arguments的正式文件

相关问题 更多 >