[英]Giveaway bot discord.py
一切正常 - 嵌入发送但计时器不起作用,并且在赠品结束时编辑消息。 这是我的错误:
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')
您在格式化时犯了一些错误。
discord.Embed
定义的,而不仅仅是Embed
await sleep
,使用await asyncio.sleep
。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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.