【问题标题】:how to make discord py bot send a message when it is invited to a server如何让discord py bot在被邀请到服务器时发送消息
【发布时间】:2021-07-23 00:22:27
【问题描述】:

我有一个 discord.py 机器人,我使用这个脚本让机器人在被邀请到服务器时向主频道发送消息! 但是,这不会向服务器发送任何内容!请帮忙!

@client.event
async def on_guild_join(guild):
    print("NEW SERVER WAS JOINED!")
    general = find(lambda x: x.name == 'general',  guild.text_channels)
    if general and general.permissions_for(guild.me).send_messages:  
        await general.send(f"""
████████╗░░░░░░██████╗░░█████╗░████████╗
╚══██╔══╝░░░░░░██╔══██╗██╔══██╗╚══██╔══╝
░░░██║░░░█████╗██████╦╝██║░░██║░░░██║░░░
░░░██║░░░╚════╝██╔══██╗██║░░██║░░░██║░░░
░░░██║░░░░░░░░░██████╦╝╚█████╔╝░░░██║░░░
░░░╚═╝░░░░░░░░░╚═════╝░░╚════╝░░░░╚═╝░░░
```Thanks For Inviting the bot!
Made with <3 by TOG6#6666 with Tons of help from the TOG6 community!```
***Server Invite:*** https://discord.gg/vSxuAbq""")
        await general.send(f"``` Type $help to view all commands!```")

【问题讨论】:

  • 你试过没有if 吗?你检查过你在general 里有什么吗?也许它有一些与你预期不同的东西,这使得if 永远不会被执行。
  • @furas 对不起,我没有正确地提出我的问题,比如我如何让它将该消息发送到服务器的主频道,服务器不需要通用频道跨度>
  • 您可以尝试打印general 变量吗?
  • 你在bot加入后尝试将消息发送到主频道,频道不必命名为general但它应该是主频道对吧?如果是这样,机器人应该如何知道主要渠道是什么? ???除非您可以让机器人检查每个频道的消息最多的频道,但这可能需要很长时间才能发送消息,并且可能会使机器人慢一点,这是不好的。主要频道更有可能被命名为一般或主要频道,所以也许您可以寻找那些或做其他。

标签: python python-3.x discord.py discord.py-rewrite


【解决方案1】:

我要感谢大家回复这篇文章!但是我想出了一个逻辑!

由于我的机器人获得了管理员权限,它可以在任何频道上发帖!

现在的问题是,只要有人说 T-BOT(区分大小写),机器人就会发布主要信息

这样,没有人会同时说出机器人的全部内容,机器人会发布它的第一条消息!

对于那些想知道如何做的人,您可以使用on_message discord 事件使其搜索机器人名称!

我希望我能帮助将来制作discord.py 机器人的人! :-)

【讨论】:

    【解决方案2】:
    @client.event
    async def on_guild_join(guild):
        for channel in guild.text_channels:
            if channel.permissions_for(guild.me).send_messages:
                await channel.send('I will send this message when I join a server')
            break
    

    【讨论】:

      【解决方案3】:

      我更擅长 discord.js,但我会给你的 discord.py 问题试一试……

      @client.event
      async def on_guild_join(guild):
          try:
              joinchannel = guild.system_channel
              # The system channel is where Discord’s join messages are sent
              await joinchannel.send('Thanks for inviting me to your server!')
          catch:
              # if no system channel is found send to the first channel in guild
              await guild.text_channels[0].send(<message>)
      

      【讨论】:

        【解决方案4】:

        我上次回答说你可以使用 on_message,但后来我想出了 2000 倍更好的东西!每个服务器都有一个系统通道,使用您的 discord.py 机器人,您可以找到系统通道并让它在那里发送消息!

        这是最好的方法,因为大多数服务器所有者将主要或重要频道作为他们的系统频道!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-17
          • 1970-01-01
          • 2020-10-26
          • 2021-09-17
          • 2018-05-03
          • 1970-01-01
          • 1970-01-01
          • 2020-11-07
          相关资源
          最近更新 更多