【问题标题】:A possible Iteration issue?可能的迭代问题?
【发布时间】:2018-10-03 16:15:11
【问题描述】:

我决定为我的机器人的右舷模块重写我的代码。 当将星形反应以外的任何反应添加到帖子中然后将消息发布到右舷时,我遇到了问题。

它的运行方式是当只有一个星形表情符号添加到帖子中时,它将添加到右舷。

我不确定这是否是简单的迭代,但这是我正在使用的

    async def on_reaction_add(self, reaction, member):
    for guild in self.bot.guilds:
        chan = get(guild.channels, name="starboard")
        if chan:
            for i in reaction.message.reactions:
                if i.emoji == '⭐':
                    return
                if reaction.message.author == member:
                    return
                if reaction.count < 1:
                    return
                embed=discord.Embed(color=0xff8000, description=reaction.message.content)
                embed.set_author(name=reaction.message.author.name, icon_url=reaction.message.author.avatar_url)
                if len(reaction.message.attachments) > 0:
                    embed.set_image(url=reaction.message.attachments[0]["url"])
                embed.set_footer(text=f"Posted in {reaction.message.channel.name}")
                embed.timestamp = dt.datetime.utcnow()
            await chan.send("New Star!", embed=embed) 

【问题讨论】:

    标签: python-3.x iteration discord.py discord.py-rewrite


    【解决方案1】:
    if i.emoji == '⭐':
        return
    

    如果表情符号是星星,请停止协程。我认为您正在寻找更多类似

    的东西
    async def on_reaction_add(self, reaction, member):
        chan = get(reaction.message.guild.channels, name="starboard")
        if not chan:
            return
        if reaction.emoji != '⭐':  # We only care about stars
            return
        if reaction.message.author == member: 
            return
        if reaction.count != 1: # Only the first time
            return
        embed=discord.Embed(color=0xff8000, description=reaction.message.content)
        embed.set_author(name=reaction.message.author.name, icon_url=reaction.message.author.avatar_url)
        if len(reaction.message.attachments) > 0:
            embed.set_image(url=reaction.message.attachments[0].url)
        embed.set_footer(text=f"Posted in {reaction.message.channel.name}")
        embed.timestamp = dt.datetime.utcnow()
        await chan.send("New Star!", embed=embed) 
    

    当有人第一次使用星号做出反应时,这会向右舷发布一条消息。目前,如果第一颗星来自消息作者,那么无论多少星都不允许它被放在右舷。我把它作为练习留给读者;-)

    【讨论】:

    • embed.set_image(url=reaction.message.attachments[0]["url"]) TypeError: 'Attachment' object is not subscriptable 我收到此错误。
    • 改用reaction.message.attachments[0].url
    猜你喜欢
    • 2021-09-13
    • 2016-06-15
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 2011-08-06
    • 1970-01-01
    相关资源
    最近更新 更多