【问题标题】:Running discord bot运行不和谐机器人
【发布时间】:2021-01-27 08:30:46
【问题描述】:

我想制作一个不和谐的机器人,然后它一直显示在不和谐包中找不到客户端。我安装了错误的不和谐包吗?

# TOKEN and CHANNEL redacted
import discord

client = discord.Client()


@client.event
async def msg():
    general = client.get_channel(CHANNEL)
    await general.send('HELLO')


client.run(TOKEN)

运行时错误:

Traceback (most recent call last):
  File "C:/Users/user/Desktop/dis.py", line 1, in <module>
    import discord
  File "C:\Users\user\Desktop\venv\lib\site-packages\discord\__init__.py", line 25, in <module>
    from .client import Client
  File "C:\Users\user\Desktop\venv\lib\site-packages\discord\client.py", line 27, in <module>
    import asyncio
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\asyncio\__init__.py", line 8, in <module>
    from .base_events import *
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 39, in <module>
    from . import coroutines
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\asyncio\coroutines.py", line 5, in <module>
    import inspect
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\inspect.py", line 35, in <module>
    import dis
  File "C:\Users\user\Desktop\dis.py", line 3, in <module>
    client = discord.Client()
AttributeError: partially initialized module 'discord' has no attribute 'Client' (most likely due to a circular import)

Process finished with exit code 1

【问题讨论】:

  • 您可能需要将文件从dis.py 重命名为其他名称,似乎发生了一些奇怪的事情。另请删除您的 Discord 令牌。
  • 你在这篇文章中泄露了你的令牌,所以我建议你生成一个新的

标签: python discord


【解决方案1】:

首先,您泄露了您的机器人的不和谐令牌,因此您应该重新生成它。

这就是我在制作机器人时所做的,

import discord
from discord.ext import commands

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

@client.event
async def hi(ctx):
    await ctx.send("HELLO")

这应该可以工作

【讨论】:

    【解决方案2】:

    首先,安装它。

    pip install discord
    

    试试这个简单的代码

    import discord
    from discord.ext import commands
    
    client = commands.Bot(command_prefix='Your_Prefix')
    
    @client.event
    async def on_ready():
        print("Bot now is online!!!")
    
    client.run("Bot token")
    

    【讨论】:

      【解决方案3】:

      答:现在你已经泄露了你的 BOT 令牌哦上帝哦 S***,请立即删除它

      乙: most likely due to a circular import

      确保您没有在任何其他导入(如果存在)中再次导入 dicord

      并确保您的文件未命名为 discord.py

      【讨论】:

        【解决方案4】:

        好的,试试这个:

        import discord
        from discord.ext import commands
        
        client = commands.Bot(command_prefix = "Your prefix")
        
        @client.event
        async def on_ready():
            general = client.get_channel(CHANNEL)
            await general.send('HELLO')
        

        如果有任何问题,请告诉我。但是,我不知道@client.event 下的代码是否有效。我还没有测试出来。希望这有帮助:) 再次:告诉我是否有任何问题。

        【讨论】:

          【解决方案5】:

          首先你需要添加你的导入!

          import discord
          from discord.ext import commands
          

          其次你需要添加你的前缀

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

          修正你的命令

          @client.command()
          async def msg(ctx):
              await ctx.send("HELLO")
          

          然后当然添加您的令牌! 完整代码:

          import discord
          from discord.ext import commands
          
          client = commands.Bot(command_prefix = "Your prefix")
          
          @client.event
          async def hi(ctx):
              await ctx.send("HELLO")
          

          我将你的 on_ready 命令设置为一个命令,因为如果你在多个服务器中拥有该机器人,你将立即被禁止使用 discord api!

          【讨论】:

            【解决方案6】:

            假设您想通过直接消息向用户发送消息,这个简单的代码就是您的答案。 您可以通过键入 --msg @(Username) (Message) 来向某人发送消息

            import discord
            from discord.ext import commands
            
            @client.command(pass_context=True, aliases=["dm", "message", "directmessage"])
            @commands.has_role("pog")
            async def msg(ctx, member: discord.Member, *, message):
                em = discord.Embed(title=f"Incoming Message from {ctx.author}", description=f"{str(message)}", color=discord.Colour.blue())
                await member.send(embed=em)
            
            client.run('TOKEN')
            

            P.S 我添加了一个嵌入,因为它看起来很酷,但你可以删除它。

            【讨论】:

              【解决方案7】:

              discord.Client() 不是函数。像这样定义客户端:

              import discord
              from discord.ext import commands
              

              您需要使用 Intent 访问某些功能,例如:on_member_join 和 on_member_remove

              intents = discord.Intents.all()    
              
              client = commands.Bot(command_prefix="!", intents=intents, owner_id=putyouridhere)
              
              @client.event
              async def on_ready():
                  print("Bot is ready.")
                  general = client.get_channel(CHANNEL)
                  await general.send('HELLO')
              
              client.run("TOKEN")
              

              【讨论】:

                【解决方案8】:

                这里的问题似乎是你使用了错误的包来获取客户端。

                要解决这个问题

                import discord
                from discord.ext import commands
                
                client = commands.Bot(command_prefix='prefix you want')
                

                希望对你有帮助

                【讨论】:

                • 这不是问题,他们正在创建一个 discord.Client 在这里你是从 commands 扩展创建一个 commands.Bot
                • ext 不是exit
                猜你喜欢
                • 2018-03-20
                • 2021-05-24
                • 1970-01-01
                • 2020-07-03
                • 2020-09-23
                • 2021-10-30
                • 2021-08-05
                • 2021-05-27
                • 2021-07-15
                相关资源
                最近更新 更多