简体   繁体   English

如何在discord.py中找到发送起始消息的默认频道?

[英]How to find a default channel to send a starting message in discord.py?

I want my bot to find the first channel which is a text channel and which he has permission to send messages in. The problem is that I don't know how to check for permissions for each channel.我希望我的机器人找到第一个频道,它是一个文本频道并且他有权发送消息。问题是我不知道如何检查每个频道的权限。

# When the bot is ready
@client.event
async def on_ready():

    # Ready message
    print("Bot is ready.")

    # Getting the default channel
    default_channel = checkChannels()


# Getting the default channel
@bot_has_permissions(send_messages=True)
def checkChannels():
    for guild in client.guilds:
            for channel in guild.channels:
                if str(channel.type) == "text":
                        return channel 

You have few possibilities.你的可能性很小。

If you want to find the system_channel (Discord's welcome messages gets send here):如果你想找到system_channel (Discord 的欢迎信息在这里发送):

@client.event
async def on_ready():
    for guild in client.guilds:
        channel = guild.system_channel #getting system channel
        if channel.permissions_for(guild.me).send_messages: #making sure you have permissions
            await channel.send("I'm online!")

This way you will find the first channel to which you have permissions :这样,您将找到您有权访问的第一个频道

@client.event
async def on_ready():
    for guild in client.guilds:
        for channel in guild.text_channels: #getting only text channels
            if channel.permissions_for(guild.me).send_messages: #checking if you have permissions
                await channel.send("I'm online!")
                break #breaking so you won't send messages to multiple channels

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM