【问题标题】:Sending messages in the on_ready? Python discord bot在 on_ready 中发送消息? Python 不和谐机器人
【发布时间】:2020-05-11 21:54:09
【问题描述】:

我希望我的机器人在 on_ready 事件中上线时发送消息。该行在(on_message)中工作,但我无法让它在(on_ready)中发送一些东西

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))
    await message.channel.send('The bot is online ')

【问题讨论】:

  • 欢迎来到stackoverflow!您发布的代码似乎没有任何问题。您能否包含更多上下文,或发布您收到的错误消息?
  • @ErtySeidohl 每次运行代码时都会出现此错误。 文件“D:/code/python/discord test/discordtest.py”,第 12 行,在 on_ready await message.channel.send('The bot is online') NameError: name 'message' is not defined。 我尝试将message.channel.send('The bot is online ') 更改为client .channel.send('The bot is online '),因为client = discord.Client() 但它也不起作用。
  • @ErtySeidohl 使用client .channel.send('The bot is online ') 我收到此错误。 文件“D:/code/python/discord test/discordtest.py”,第 11 行,在 on_ready await client.channel.send('The bot is online') AttributeError: 'Client' object has no attribute 'channel '

标签: python bots discord python-asyncio


【解决方案1】:

复制您的 user_id 并粘贴不带引号

    @client.event
    async def on_ready():
        print("The bot is ready")
        print("------------------------")
        user = await client.fetch_user("User id")
        await user.send("hello")
            

【讨论】:

    【解决方案2】:

    discord.py 似乎没有将变量作为通道

    @client.event
    async def on_ready():
       await client.get_channel("enter channel id here").send("bot is online")
    

    我刚刚试了一下,效果很好

    【讨论】:

      【解决方案3】:

      您没有选择要向其发送消息的频道。首先,您需要选择一个频道,然后您可以向该频道发送消息。

      channel = client.get_channel(12324234183172)
      await channel.send('hello')
      

      您可以通过遍历连接服务器中的频道列表来获取频道 ID:

      text_channel_list = []
      for server in Client.servers:
          for channel in server.channels:
              if channel.type == 'Text':
                  text_channel_list.append(channel)
      

      How to get all text channels using discord.py?

      来自discord python FAQ中的How do I send a message to a specific channel?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-02
        • 2019-12-10
        • 1970-01-01
        • 2021-04-03
        • 1970-01-01
        • 2020-10-25
        • 2018-05-02
        • 1970-01-01
        相关资源
        最近更新 更多