Discord.py send()接受1到2个位置参数

2024-10-03 00:23:02 发布

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

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

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: send() takes from 1 to 2 positional arguments but 6 were given

显示在控制台中。我已经试过了,比如把它放在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)')

Tags: clientsend编码asyncifdefcheckchannel
1条回答
网友
1楼 · 发布于 2024-10-03 00:23:02

这个代码非常糟糕

  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)

^{}

相关问题 更多 >