【问题标题】:Discord Python Rewrite - Giveaway Command in Cogs ErrorDiscord Python Rewrite - Cogs 错误中的赠品命令
【发布时间】:2020-09-08 19:38:08
【问题描述】:

所以,我在 cogs 中制作了赠品代码,代码是:

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”对象没有“用户”属性

【问题讨论】:

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


    【解决方案1】:

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

    以下代码将刷新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))
    

    【讨论】:

    • 嘿,我明天试试那个代码,因为现在是深夜,谢谢你的遮阳篷!
    猜你喜欢
    • 2021-04-02
    • 1970-01-01
    • 2021-05-22
    • 2021-06-02
    • 2020-03-17
    • 2021-03-20
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多