【问题标题】:Discord Bot Command is not ShowingDiscord Bot 命令未显示
【发布时间】:2021-01-21 15:45:46
【问题描述】:

所以我正在制作一个基本的机器人命令,以响应玩家所说的内容,例如执行 !test code 将使机器人以“代码”响应。出于某种原因,运行命令时没有任何反应。我什至在里面放了一个打印件,看看它是否真的在运行,但事实并非如此。这是我的代码:

import discord
from discord.ext import commands

client = discord.Client()
bot = commands.Bot(command_prefix="!")

@client.event
async def on_ready():
    print('Logged in as {0.user}'.format(client))
    print("-"*16)

game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)

@bot.command()
async def test(ctx, arg):
    await ctx.send(str(arg))

client.run('token here')

感谢任何帮助。

【问题讨论】:

    标签: python discord


    【解决方案1】:

    试试这个:

    import discord
    from discord.ext import commands
    
    client = commands.Bot(command_prefix="!")
    
    @client.event
    async def on_ready():
        print('Logged in as {0.user}'.format(client))
        print("-"*16)
    
    game = discord.Game("Discord")
    await client.change_presence(status=discord.Status.online, activity=game)
    
    @client.command()
    async def test(ctx, *, arg):
        await ctx.send(str(arg))
    
    client.run('token here')
    
    

    你错了:

    client = discord.Client()
    bot = commands.Bot(command_prefix="!")
    

    你有 2 个单独的机器人处理程序,如果你使用命令,你只需要 bot = commands.Bot(command_prefix="!") 行,在这种情况下,你有命令的机器人处理程序,但你正在运行客户端

    【讨论】:

    • 我认为上面的代码会抛出“'await' outside function”错误......你测试过你的答案吗
    • 是的,它奏效了。我真的很困惑,因为只有 2 个更改:变量名更改和添加的星号。还有我添加回来的函数之外的等待。
    • 是的,我会编辑答案以向您展示您做错了什么:)
    【解决方案2】:

    如果想让机器人运行最后一行代码必须是 bot.run('your token') 如果你想让客户端运行使用 client.run('your token'),你不能同时运行客户端和机器人/p>

    【讨论】:

      【解决方案3】:

      以下代码在使用 Python3.7 测试时按预期运行 ...

      import discord
      from discord.ext import commands
      
      client = commands.Bot(command_prefix="!")
      
      @client.event
      async def on_ready():
          print('Logged in as {0.user}'.format(client))
          print("-"*16)
      
          game = discord.Game("Discord")
          await client.change_presence(status=discord.Status.online, activity=game)
      
      @client.command()
      async def test(ctx, *arg):
          await ctx.send(str(arg))
      
      client.run('token here')
      

      您发布的代码在函数之外有一个等待语句

      ......
      print("-"*16)
      
      game = discord.Game("Discord")
      await client.change_presence(status=discord.Status.online, activity=game)
      .......
      

      也改变

      @bot.command()
      async def test(ctx, arg):
      

      到:

      @bot.command()
      async def test(ctx, *arg):
      

      关于你为什么通过*arg而不是arg的解释:

      args-and-kwargs

      【讨论】:

        猜你喜欢
        • 2018-03-04
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        • 2020-10-25
        • 2018-10-21
        • 2021-03-01
        • 1970-01-01
        • 2021-08-07
        相关资源
        最近更新 更多