赠送机器人discord.py

2024-04-27 00:15:02 发布

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

一切正常-嵌入发送,但定时器不工作,编辑消息,而赠品结束。这是我的错误:

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

Tags: inpylinemsgembedawaitusersext
1条回答
网友
1楼 · 发布于 2024-04-27 00:15:02

格式化时您犯了一些错误

  1. 嵌入是用discord.Embed定义的,而不仅仅是Embed
  2. await sleep不应使用,而应使用await asyncio.sleep
  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)

因此,这里的新功能是,我们实际上fetch通过ID调用消息,这是一个API调用,但是我们得到了我们需要的所有信息

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

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

相关问题 更多 >