首页 > 解决方案 > Discord.py send() 接受 1 到 2 个位置参数

问题描述

我尝试使用 discord.py 编写投票系统我希望机器人发送用户在我发现之前发送的消息我可以通过编码来做到这一点async def voting(ctx, *text):并将 * 符号放在文本参数的前面但是当我尝试对机器人进行编码,以便他向文本参数发送错误:

discord.ext.commands.errors.CommandInvokeError:命令引发异常:TypeError:send() 需要 1 到 2 个位置参数,但给出了 6 个

出现在控制台中。我已经尝试过将其放入 f 字符串中,但它不起作用。

这是此命令的完整代码

@client.command()
async def voting(ctx, *text):
    await ctx.channel.purge(limit = 1)
    message = await ctx.send(*text)

    cross = client.get_emoji(790243377953636372)
    check = client.get_emoji(790243459050110977)
    voting_cross = 0
    voting_check = 0

    await client.add_reaction(message, emoji = cross)
    await client.add_reaction( emoji = check )

    @client.event
    async def on_reaction_add(reaction, user):

        reaction_channel = reaction.message.channel
        voting_channel = client.get_channel(voting_channel_id)

        if reaction_channel == voting_channel :

            if str(reaction.emoji) == "✅":

                voting_check = voting_check + 1
                print(f'{user} has votet with ')

            if str(reaction.emoji) == "❌":

                voting_cross = voting_cross + 1
                print(f'{user} has votet with ')

    @client.command()
    async def results(ctx):

        if voting_check > voting_cross :
            await ctx.send(f'More people votet for :greencheckmark: ({voting_check} votes)')

        else :
            await ctx.send(f'More people votet for :redcross: ({voting_cross} votes)')

标签: pythondiscorddiscord.py

解决方案


这段代码真的很糟糕。

  1. 你是在打开一个列表,而不是加入它
>>> lst = [1, 2, 3]
>>> print(lst)
[1, 2, 3]
>>> print(*lst)
1 2 3 # It's not the same, you need to join it using str.join(list)
>>> ' '.join(lst)
'1 2 3'

此外,如果您想将其作为字符串传递,请使用:

@client.command()
async def voting(ctx, *, text):
  1. client.add_reaction它不再是一个东西,如果你正在使用discord.py 1.0+Message.add_reaction
await message.add_reaction(whatever)
  1. 您不要将事件放在命令中,而是使用client.wait_for(event),这是一个示例
@client.command()
async def voting(ctx, *text):
    # add the reactions to the message here or whatever

    # Here's how to wait for a reaction
    def check_reaction(reaction, user):
        return user == ctx.author

    reaction, user = await client.wait_for('message', check=check_reaction)

    # Here's how to wait for a message
    def check_message(message):
        return message.author == ctx.author

    message = await client.wait_for('message', check=check_message)

wait_for


推荐阅读