【问题标题】:I made a afk command for my bot in discord.py but when I do the Afk command it is only 1 Word when pinged我在 discord.py 中为我的机器人创建了一个 afk 命令,但是当我执行 Afk 命令时,ping 时它只有 1 个字
【发布时间】:2021-07-25 03:53:15
【问题描述】:

我在 discord.py 中为我的机器人创建了一个 afk 命令,但是当我执行 Afk 命令时,ping 代码时只有 1 个字,代码就在这里https://nekobin.com/sulotolise

@client.event
async def on_member_join(member):
    print(f'{member} has joined a server!')
    with open('afk.json', 'r') as f:
        afk = json.load(f)

    await update_data(afk, member)

    with open('afk.json', 'w') as f:
        json.dump(afk, f)


async def update_data(afk, user):
    if not f'{user.id}' in afk:
        afk[f'{user.id}'] = {}
        afk[f'{user.id}']['AFK'] = 'False'


@client.event
async def on_message(self, message):
        with open('db/afk.json', 'r') as f:
            afk = json.load(f)
        
        for user_mention in message.mentions:
            if afk[f'{user_mention.id}']['AFK'] == 'True':
                if message.author.bot: 
                    return
                
                reason = afk[f'{user_mention.id}']['reason']
                meth = int(time.time()) - int(afk[f'{user_mention.id}']['time'])
                been_afk_for = await self.time_formatter(meth)
                embed = discord.Embed(description=f'{user_mention.name} Is currently AFK!\nReason : {reason}')
                await message.channel.send(content=message.author.mention, embed=embed)
                
                meeeth = int(afk[f'{user_mention.id}']['mentions']) + 1
                afk[f'{user_mention.id}']['mentions'] = meeeth
                with open('db/afk.json', 'w') as f:
                    json.dump(afk, f)
        
        if not message.author.bot:
            await self.update_data(afk, message.author)

            if afk[f'{message.author.id}']['AFK'] == 'True':
                
                meth = int(time.time()) - int(afk[f'{message.author.id}']['time'])
                been_afk_for = await self.time_formatter(meth)
                mentionz = afk[f'{message.author.id}']['mentions']

                embed = discord.Embed(description=f'Welcome Back {message.author.name}!', color=0x00ff00)
                embed.add_field(name="You've been AFK for :", value=been_afk_for, inline=False)
                embed.add_field(name='Times you got mentioned while you were afk :', value=mentionz, inline=False)

                await message.channel.send(content=message.author.mention, embed=embed)
                
                afk[f'{message.author.id}']['AFK'] = 'False'
                afk[f'{message.author.id}']['reason'] = 'None'
                afk[f'{message.author.id}']['time'] = '0'
                afk[f'{message.author.id}']['mentions'] = 0
                
                with open('db/afk.json', 'w') as f:
                    json.dump(afk, f)
                
                try:
                    await message.author.edit(nick=f'{message.author.display_name[5:]}')
                except:
                    print(f'I wasnt able to edit [{message.author}].')
        
        with open('afk.json', 'w') as f:
            json.dump(afk, f)
        
@client.command()
async def afk(ctx, *, self, reason=None):
        with open('db/afk.json', 'r') as f:
            afk = json.load(f)

        if not reason:
            reason = 'None'
        
        await self.update_data(afk, ctx.message.author)
        afk[f'{ctx.author.id}']['AFK'] = 'True'
        afk[f'{ctx.author.id}']['reason'] = f'{reason}'
        afk[f'{ctx.author.id}']['time'] = int(time.time())
        afk[f'{ctx.author.id}']['mentions'] = 0

        embed = discord.Embed(description=f"I've set your AFK {ctx.message.author.name}!\nReason :{reason}", color=0x00ff00)
        await ctx.send(content=ctx.message.author.mention, embed=embed)

        with open('db/afk.json', 'w') as f:
            json.dump(afk, f)
        try:
            await ctx.author.edit(nick=f'[AFK]{ctx.author.display_name}')
        except:
            print(f'I wasnt able to edit [{ctx.message.author}].')

图片

【问题讨论】:

  • 请在此处包含代码。
  • 您需要添加代码,以便其他人可以帮助您。

标签: python discord discord.py bots


【解决方案1】:

我认为你在发出命令时犯了一个小错误。第72行的星号应该是背后的原因:

@client.command()
async def afk(ctx, *, reason=None):

【讨论】:

  • 另一个问题是他不应该把self 参数。
  • 哦,谢谢你这是一个愚蠢的错误,这是修复它的原因,谢谢
  • 很高兴我能提供帮助。你能用那个小复选标记接受我的回答吗? ^^ @Gøne。
猜你喜欢
  • 2021-01-22
  • 2021-05-02
  • 2021-07-15
  • 2021-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-02
相关资源
最近更新 更多