【问题标题】:Giveaway bot discord.py赠品机器人 discord.py
【发布时间】:2021-05-25 21:59:36
【问题描述】:

一切正常 - 嵌入发送但计时器不起作用,并且在赠品结束时编辑消息。这是我的错误:

Ignoring exception in command giveaway:
Traceback (most recent call last):
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\Users\HP\Desktop\222\dscbot-dscbot.py", line 70, in giveaway
    users = await msg.reactions[0].users().flatten()
IndexError: list index out of range

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: IndexError: list index out of range

如果有人可以发送此代码但已编辑,我将不胜感激。我知道这个问题可能不清楚我是 discord.py 的新手。

from asyncio import sleep
from discord.ext import commands
import discord
from discord import Embed, TextChannel

intents = discord.Intents.default()

intents.members = True
client = commands.Bot(command_prefix = "-", intents = intents)



@client.command()
@commands.guild_only()
@commands.has_permissions(administrator=True)
async def giveaway(ctx, duration: int, channel: discord.TextChannel, *, prize: str):
    reaction = discord.Reaction
    embed = Embed(title=prize,
                  description=f"Hosted by - {ctx.author.mention}\nReact with :tada: to enter!\nTime Remaining: **{duration}** seconds",
                  color=ctx.guild.me.top_role.color,)

    msg = await ctx.channel.send(content=":tada: **GIVEAWAY** :tada:", embed=embed)
    await msg.add_reaction("????")

    users = await msg.reactions[0].users().flatten()
    users.pop(users.index(ctx.guild.me))
    

    while duration:
        await sleep(2)
        duration -= 2
        embed.description = f"Hosted by - {ctx.author.mention}\nReact with :tada: to enter!\nTime Remaining: **{duration}** seconds"
        await msg.edit(embed=embed)
    winner = random.choice(users)
    await ctx.send(f"**Congrats to: {winner}!**")
    embed.description = f"Winner: {winner.mention}\nHosted by: {ctx.author.mention}"
    await msg.edit(embed=embed)
client.run('TOKEN')

【问题讨论】:

  • 你能添加整个回溯吗?
  • 添加了@ŁukaszKwieciński
  • 欢迎来到 StackOverflow !提出问题的目的不是让其他人为您编写代码,而是帮助您(和其他人)更好地了解发生了什么以及使其工作的解决方案。您能否通过生成 minimal, reproducible example 而不是整个代码来帮助我们?
  • @carmeldev 这似乎不是完整的追溯
  • 关于包含完整回溯的cmets是因为回溯包含诊断和解决问题的有价值信息。虽然错误消息很有帮助,但回溯包含各种其他信息,例如导致错误的行。

标签: python discord discord.py


【解决方案1】:

您在格式化时犯了一些错误。

  1. 嵌入是用discord.Embed 定义的,而不仅仅是Embed
  2. await sleep 不应使用,请使用 await asyncio.sleep insted。
  3. reaction = discord.Reaction 不是真正的调用,甚至没有在您的代码中使用,我删除的 channel: discord.TextChannel 也是如此。

您似乎还以错误的方式请求响应。我遇到了类似的问题,并稍微更改了您的代码。

我们现在使用不同的方法,而不是 users = await msg.reactions[0].users().flatten()

import asyncio
from discord.ext import commands
import discord
import random

@client.command()
@commands.guild_only()
@commands.has_permissions(administrator=True)
async def giveaway(ctx, duration: int, *, prize: str):
    embed = discord.Embed(title=prize,
                          description=f"Hosted by - {ctx.author.mention}\nReact with :tada: to enter!\nTime Remaining: **{duration}** seconds",
                          color=ctx.guild.me.top_role.color, )

    msg = await ctx.channel.send(content=":tada: **GIVEAWAY** :tada:", embed=embed)
    await msg.add_reaction("?")
    await asyncio.sleep(10)
    new_msg = await ctx.channel.fetch_message(msg.id)

    user_list = [u for u in await new_msg.reactions[0].users().flatten() if u != client.user] # Check the reactions/don't count the bot reaction

    if len(user_list) == 0:
        await ctx.send("No one reacted.") 
    else:
        winner = random.choice(user_list)
        e = discord.Embed()
        e.title = "Giveaway ended!"
        e.description = f"You won:"
        e.timestamp = datetime.datetime.utcnow()
        await ctx.send(f"{winner.mention}", embed=e)

所以这里的新功能是我们实际上是通过 ID 来fetch 消息,这是一个 API 调用,但获取了我们需要的所有信息。

如果您只想编辑已发布的消息,只需使用以下命令:

await new_msg.edit(content=f"{winner.mention}", embed=e)

【讨论】:

  • 谢谢先生,但是有没有办法编辑这个嵌入?
  • 我收到此错误 AttributeError: 'Command' object has no attribute 'to_dict'
  • 您是否复制了代码并仅编辑了最后一行?如果是这样,情况就不应该如此。
猜你喜欢
  • 2021-03-20
  • 2021-05-02
  • 1970-01-01
  • 2021-11-01
  • 2021-05-03
  • 2021-06-02
  • 2021-07-20
  • 2021-05-08
  • 1970-01-01
相关资源
最近更新 更多