discord.py - 如何在机器人启动时向它所在的每台服务器发送消息?

标签 discord.py

所以我想向我的机器人所在的所有服务器发送一个公告。我在 github 上找到了这个,但它需要一个服务器 id 和一个 channel id。

@bot.event
async def on_ready():
    server = bot.get_server("server id")
    await bot.send_message(bot.get_channel("channel id"), "Test")

我也发现了一个类似的问题,但它在 discord.js 中。它说的是默认 channel ,但是当我尝试时:
@bot.event
async def on_ready():
    await bot.send_message(discord.Server.default_channel, "Hello everyone")

它给了我错误:
Destination 必须是 Channel、PrivateChannel、User 或 Object

最佳答案

首先回答您关于default_channel的问题:大约从 2017 年 6 月起,discord 不再定义“默认” channel ,因此,default_channel服务器的元素通常设置为无。

其次,说discord.Server.default_channel ,您要求的是类定义的元素,而不是实际的 channel 。要获得实际的 channel ,您需要一个服务器实例。

现在要回答最初的问题,即向每个 channel 发送消息,您需要在服务器中找到一个可以实际发布消息的 channel 。

@bot.event
async def on_ready():
    for server in bot.servers: 
        # Spin through every server
        for channel in server.channels: 
            # Channels on the server
            if channel.permissions_for(server.me).send_messages:
                await bot.send_message(channel, "...")
                # So that we don't send to every channel:
                break

关于discord.py - 如何在机器人启动时向它所在的每台服务器发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48069868/

相关文章:

python - 如何在 python discord bot 中获取用户输入?

python - Discord.py 如何删除服务器中的所有角色

python - Discord.py 如何删除每个文本 channel ?

python - 使用本地文件作为 set_image 文件 discord.py

python-3.x - 如何获取机器人头像链接

python - 如何确保用户在discord.py 中输入完整命令?

python - Discord.py on_member_join 不工作,没有错误信息

python-3.x - Discord.py 重写 - YoutubeDL 播放音乐的来源是什么?

python - 如何在python中使用多线程停止正在运行的进程

python - Nextcord:无法访问服务器上的 ffmpeg