Discord Python重写-Cogs错误中的赠品命令

时间:2020-09-08 09:34:24

标签: python discord.py discord.py-rewrite

所以,我正在用齿轮制作一个赠品代码,代码是:

import discord
import datetime
import time
import asyncio
import random
import random

from discord.ext import commands
from discord import Embed

class Giveaway(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command()
    async def giveaway(self, ctx, time: int, *, prize):
        giveawayembed = discord.Embed(
            title="? New Giveaway! ?",
            colour=discord.Color.green()
            )

        giveawayembed.add_field(name="Prize", value="{}".format(prize), inline=False)
        giveawayembed.add_field(name="Hosted by", value=f"{ctx.author.mention}", inline=False)
        giveawayembed.add_field(name="Ends in", value="{}s".format(time))

        msg = await ctx.send(embed=giveawayembed)

        reactions = await msg.add_reaction("?")

        await asyncio.sleep(time)

        async for users in reactions.users(0):
            users = await reaction.users().flatten()
            winner = random.choice(users)

        endembed = discord.Embed(
            title="Giveaway ended!",
            description="Prize: {}\nWinner: {}".format(prize, winner))

        await msg.edit(embed=endembed)

    @giveaway.error
    async def giveaway_error(self, ctx, error):
        await ctx.send(error)
        print(error)
        raise error

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

但是当我尝试时,在作品中,但是获得获胜者时出现了一些错误 错误:

discord.ext.commands.errors.CommandInvokeError:命令引发了异常:AttributeError:'NoneType'对象没有属性'users'

1 个答案:

答案 0 :(得分:1)

message.add_reaction协程不返回任何内容,这意味着reactions = await msg.add_reaction("?")将导致reactionsNone

下面的代码将刷新message对象并覆盖msg,因为这对于在消息上产生新的响应是必需的。

它还将检查是否选择了获胜者(以防万一没有人进入赠品),并从能够赢取赠品的机器人帐户中删除。

import discord
import datetime
import time
import asyncio
import random

from discord.ext import commands
from discord import Embed

class Giveaway(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command()
    async def giveaway(self, ctx, time: int, *, prize):
        giveawayembed = discord.Embed(
            title="? New Giveaway! ?",
            colour=discord.Color.green()
            )

        giveawayembed.add_field(name="Prize", value="{}".format(prize), inline=False)
        giveawayembed.add_field(name="Hosted by", value=f"{ctx.author.mention}", inline=False)
        giveawayembed.add_field(name="Ends in", value="{}s".format(time))

        msg = await ctx.send(embed=giveawayembed)

        await msg.add_reaction("?")

        await asyncio.sleep(time)

        msg = await msg.channel.fetch_message(msg.id)
        winner = None
        
        for reaction in msg.reactions:
            if reaction.emoji == "?":
                users = await reaction.users().flatten()
                users.remove(self.client.user)
                winner = random.choice(users)

        if winner is not None:
            endembed = discord.Embed(
                title="Giveaway ended!",
                description="Prize: {}\nWinner: {}".format(prize, winner))

            await msg.edit(embed=endembed)

    @giveaway.error
    async def giveaway_error(self, ctx, error):
        await ctx.send(error)
        print(error)
        raise error

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