Discord.py 即使用户不在我运行命令的服务器中,如何从 cog 中的用户 ID DM 用户?

时间:2021-03-14 00:43:48

标签: discord.py

我正在处理一个删除错误的命令,然后用 bug report token (bugreport) 奖励用户 然后是他们。 那么,即使该用户不在我运行此命令的服务器中,我如何获得用户 ID? 我用于获取和 dming 用户的代码:

//user is gotten from a query, it is a real ID
userobj = ctx.guild.get_user(user)
dm = await userobj.create_dm()
msg = 'msg'
dm.send(msg)
    @commands.command(name='fixbug', allises=['finishbug'],help='[Owner Only] <bugid> <reward?>')
    @commands.is_owner()
    async def killbug(self, ctx, bugid: int, reward: str = 'False', msg: str = None):
        print(bugid)
        query = await db.load(
            f'''
                SELECT * FROM buglogs
                WHERE fixedid = {bugid}
            ''')
        print(query)
        query = query[0]
        await db.query(
            f'''
                DELETE FROM buglogs
                WHERE fixedid = {bugid}
            ''')
        
        user = query[1]
        name = query[3]
        log = query[2]
        userobj = ctx.guild.get_user(user)
        
        dm = await userobj.create_dm()
        if reward == 'True' or reward == 'true' or reward == 'yes':
            await self.add_item(user, 'bugreport', 1)
            await ctx.send(f'Gave {name} A Bug Report Token, I also squashed \"{log}\" bug!')
            
            await dm.send(f"Thank you for reporting the bug: \"{log}, You have been rewarded 1 Bug Report Token, sell it for your reward!")
        else:
            await ctx.send('Squashed: {log}')
            if msg == None:
                dm.send('The bug you reported ({log}) has been resolved, thank you!')
            else:
                dm.send(msg)

1 个答案:

答案 0 :(得分:1)

您可以使用bot.get_user

discordUser = self.bot.get_user(user) #pass user id here
if discordUser is not None:
    #do stuff
else:
    await ctx.send('user not found')

警告:此代码假设您在一个 cog 中并在 init 中设置了 self.bot = bot,就像在 docs

参考: