discord.py机器人在本地运行,但在heroku上托管时无法运行

时间:2020-09-27 16:00:11

标签: python heroku discord discord.py

我已经在Python中创建了一个不和谐角色分配机器人,并按预期在本地运行代码。

#import discord lib
import discord
#import commands from discord lib
from discord.ext import commands

client = commands.Bot(command_prefix = ".")

#start event and check Bot is ready
@client.event
async def on_ready():
    print("Bot is ready.")

#upon detecting a reaction event assign the role which matches the reaction emoji
#see https://discordpy.readthedocs.io/en/latest/api.html?#rawreactionactionevent
@client.event
async def on_raw_reaction_add(payload):
    message_id = payload.message_id

    #check if the message id matches the message id of the "assign your role!" message in discord
    if message_id == 123456789:
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)

        #if the name of the reaction emoji is "League" theen assign the "League bois discord role
        if payload.emoji.name == "League":
            role = discord.utils.get(guild.roles, name="League bois")
        else:
        #set role variable to the name of the reaction emoji
            role = discord.utils.get(guild.roles, name=payload.emoji.name)

        if role is not None:
            member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
            #debugging
            print("member name: "+str(member))
            print("emoji name: "+str(payload.emoji.name))
            print("role name: "+str(role))
            
            #user id from event payload
            print(payload.user_id)
            #use user id from payload to search for member
            member2 = guild.get_member(payload.user_id)
            #print the search result
            print(member2)
            print(str(payload.member))
            
            #end debugging
            if member is not None:
                #add the role
                await member.add_roles(role)
                print("done")
            else:
                print("Member not found.")
        else:
            print("Role not found.")

我正在使用Github Desktop推送到Heroku。 在Visual Studio Code中进行调试会返回预期的输出:

Bot is ready.
member name: user#1234
emoji name: League
role name: League bois
987654321
user#1234
done

但是Heroku日志的调试结果表明,它找不到成员。

2020-09-27T15:21:56.088983+00:00 app[worker.1]: member name: None
2020-09-27T15:21:56.089001+00:00 app[worker.1]: emoji name: League
2020-09-27T15:21:56.089007+00:00 app[worker.1]: role name: League bois
2020-09-27T15:21:56.089008+00:00 app[worker.1]: 987654321
2020-09-27T15:21:56.089009+00:00 app[worker.1]: None
2020-09-27T15:21:56.089012+00:00 app[worker.1]: Member not found.

我相信从Heroku运行时,我的第一个代码段中的第41行失败。

 member2 = guild.get_member(payload.user_id)

我注意到print(str(payload.member))在Heroku日志中返回REACTION_ADD事件类型的成员。但是,我希望用户能够通过删除其反应和有效负载来删除其角色。member仅在event_type为REACTION_ADD而非REACTION_REMOVE时可用。

非常感谢您的帮助!

0 个答案:

没有答案