首页 > 解决方案 > 我的赠品 cog 命令由于某种原因无法正常工作

问题描述

所以我在一个 cog 中做了一个赠品命令,由于某种奇怪的原因它不起作用。我没有收到任何错误或任何东西。由于它是“交互式的”,所以前两个问题可以正常工作,但是一旦它要求奖励,它就会冻结并且之后不会做任何事情。我的朋友也在开发一个不同的机器人,并给了我他的代码,该代码在他的终端上运行良好,但在我的终端上却不行。这是我的代码:

import discord
from discord.ext import commands
from discord.ext.commands import BucketType, cooldown, CommandOnCooldown
import random
import json
import datetime
import asyncio

class Giveaway(commands.Cog):
    """Giveaway commands. You must have manage message permissions to use these"""

    def __init__(self, bot):
        self.bot = bot



    # Helper functions

    def convert(self, time):
        pos = ["s", "m", "h", "d"]

        time_dict = {"s": 1, "m": 60, "h": 3600, "d": 2600*24}

        unit = time[-1]

        if unit not in pos:
            return -1
        try:
            val = int(time[:-1])
        except:
            return -2

        return val * time_dict[unit]



    # Bot Events



    # Bot Commands

    @commands.command()
    @commands.has_permissions(kick_members=True)
    async def giveaway(self, ctx, host: discord.Member):

        await ctx.send("Let's start with this giveaway! Answer these questions within 15 seconds!")

        questions = ["Which channel should it be hosted in?",
                     "What should be the duration of the giveaway? (s|m|h|d)",
                     "What is the prize of the giveaway?"]

        answers = [f"{ctx.channel.mention}"]

        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.bot.wait_for('message', timeout=15.0, check=check)
            except asyncio.TimeoutError:
                await ctx.send('You didn\'t answer in time, please be quicker next time!')
                return
            else:
                answers.append(msg.content)

        try:
            c_id = int(answers[0][2:-1])
        except:
            await ctx.send(f"You didn't mention a channel properly smh")
            return

        channel = self.bot.get_channel(c_id)

        time = convert(answers[2])

        prize = answers[3]

        await ctx.send(f"The giveaway for {prize} will be in {channel.mention} and will last {answers[2]}!! Hosted by {host.mention}")

        embed = discord.Embed(title="Giveaway!", description=f"{prize}", color=discord.Colour.dark_purple())

        embed.add_field(name="Hosted by:", value=f"{host}")

        embed.set_footer(text=f"Ends {answers[2]} from now!")

        my_msg = await channel.send(embed=embed)

        await my_msg.add_reaction("")

        await asyncio.sleep(time)

        new_msg = await channel.fetch_message(my_msg.id)

        users = await new_msg.reactions[0].users().flatten()
        users.pop(users.index(self.bot.user))

        winner = random.choice(users)

        await channel.send(f"Congratulations! {winner.mention} won the prize: {prize} from {host.mention}!!")



def setup(bot):
    bot.add_cog(Giveaway(bot))

请让我知道我应该怎么做才能解决它!

标签: discord.py

解决方案


推荐阅读