【发布时间】:2020-12-21 02:40:05
【问题描述】:
所以我试图通过观看 yt 的一些教程来制作赠品命令 我得到一个错误....实际上我在代码中定义了 convert,但它显示错误 Command 引发了异常:NameError: name 'convert' is not defined。但是我观看的教程没有错误.....而且我正在使用 discord.py rewrite........任何帮助都得到了帮助:)。
代码:
class Giveaway(commands.Cog):
def __init__(self, client):
self.client = client
def convert(time):
pos = ["s", "m", "h", "d"]
time_dict = {"s" : 1, "m" : 60, "h" : 3600, "d" : 3600*24}
unit = time[-1]
if unit not in pos:
return -1
try:
val = int(time[:-1])
except:
return -2
return val * time_dict[unit]
@commands.command()
async def giveaway(self, ctx):
await ctx.send("Answer The Following Questions For Giveaway:")
questions = ["Channel For Giveaway?",
"Duration Of Giveaway? (s , m , h , d)",
"Prize Of Giveaway?"]
answers = []
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
for i in questions:
await ctx.send(i)
try:
msg = await self.client.wait_for('message', timeout=20.0, check=check)
except asyncio.TimeoutError:
await ctx.send('You Did Not Answer In Time!')
return
else:
answers.append(msg.content)
try:
c_id = int(answers[0][2:-1])
except:
await ctx.send(f'Mention Channel Properly! Example: {ctx.channel.mention}')
return
channel = self.client.get_channel(c_id)
time = convert(answers[1])
if time == -1:
await ctx.send(f'Answer Time With A Proper Unit (s, m, h, d)')
return
elif time == -2:
await ctx.send(f'Time Must Be A Integer!')
return
prize = answers[2]
await ctx.send(f'Giveaway Will Be In Channel {channel.mention} And Will Last {time}.')
embed = discord.Embed(title="????Giveaway????", description=f"{prize}", color=ctx.author.color)
embed.add_field(name="Hosted by:", value= ctx.author.mention)
embed.set_footer(text=f"Ends after {answers[1]} from now. ")
g_msg = await channel.send(embed=embed)
await g_msg.add_reaction("????")
await asyncio.sleep(time)
new_msg = await channel.fetch_message(g_msg.id)
users = await g_msg.reactions[0].users().flatten()
users.pop(users.index(self.client.user))
winner = random.choice(users)
await channel.send(f'Congrats! {winner.mention} won {prize}!')
错误:
time = convert(answers[1])
NameError: name 'convert' is not defined
和
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'convert' is not defined
【问题讨论】:
标签: python python-3.x discord discord.py discord.py-rewrite