简体   繁体   English

使用 discord.py 获取频道名称

[英]get the name of a channel using discord.py

how do I get the name of a channel so that this bot will work on any server its put on with no changes to code necessary?如何获取频道的名称,以便该机器人可以在其放置的任何服务器上工作,而无需更改代码? ( in the code where I put "what do I put here" is where I want the name to be in a variable)Thanks (在我放“我放什么”的代码中是我希望名称出现在变量中的位置)谢谢

from discord.ext.commands import Bot
import time, asyncio

TOKEN = 'Its a secret'
BOT_PREFIX = ["!"]
client = Bot(command_prefix=BOT_PREFIX)




@client.event
async def on_message(message):
    if message.author == client.user:
        return




@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    await start()
    while True:
        currentTime = time.strftime("%M%S", time.gmtime(time.time()))
        if currentTime == "30:00":
            await start()
        await asyncio.sleep(1)


async def start():
    mainChannel = #What do i put here?
    print(mainChannel.name)
    await client.send_message(mainChannel, "Starting countdown", tts = True)



client.run(TOKEN)

Getting channel from ID (Recommended)从 ID 获取频道(推荐)

First, get the ID of the channel (Right click the channel and select "Copy ID")首先,获取频道的ID(右键单击频道并选择“复制ID”)

Second, put the ID in the following code:其次,将ID放入以下代码:

client.get_channel("ID")

For example:例如:

client.get_channel("182583972662")

Note: The channel ID is a string in discord.py async, and an integer in rewrite注意: discord.py async 中的频道ID 是一个字符串,rewrite 中是一个整数

(Thanks to Ari24 for pointing this out) (感谢 Ari24 指出这一点)

Getting channel from name (Not reccomended)从名称获取频道(不推荐)

First, get the server using either:首先,使用以下任一方法获取服务器:

server = client.get_server("ID")

OR或者

for server in client.servers:
    if server.name == "Server name":
        break

Second, get the channel:二、获取渠道:

for channel in server.channels:
    if channel.name == "Channel name":
        break

What not to do什么不该做

Try to always use the ID for each server, as it is much faster and more efficient.尝试始终为每个服务器使用 ID,因为它更快、更高效。

Try to avoid using discord.utils.get, such as:尽量避免使用discord.utils.get,例如:

discord.utils.get(guild.text_channels, name="Channel name")

Although it does work, it is bad practise as it has to iterate through the entire list of channels.尽管它确实有效,但这是不好的做法,因为它必须遍历整个频道列表。 This can be slow and take much more time than using the ID.与使用 ID 相比,这可能会很慢并且花费更多的时间。

From the discord API docs:来自不和谐 API 文档:

discord.utils.get is a helper that returns the first element in the iterable that meets all the traits passed in attrs discord.utils.get 是一个帮助器,它返回迭代中满足所有在 attrs 中传递的特征的第一个元素

Now in rewrite there's a method called discord.utils.get where you can actually getting discord objects with specific parameters现在重写有一个名为discord.utils.get的方法,您可以在其中实际获取具有特定参数的不和谐对象

In your case with a channel name:在您使用频道名称的情况下:

import discord
channel = discord.utils.get(guild.text_channels, name="Name of channel")

Should be None if discord couldn't find a textchannel with that name如果不和谐找不到具有该名称的文本频道,则应为无

It is actually really easy: You can simply do message.channel.name其实很简单:你可以简单地做message.channel.name

Example:例子:

print(message.channel.name)

print(message.channel.name) 仅在您收到消息时才有效,但我想发送一个而不需要先接收一个。

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

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