问题描述
如何获取频道的名称,以便该机器人可以在其放置的任何服务器上运行,而无需更改代码?(在我放我在这里放什么"的代码中,我希望名称出现在变量中)谢谢
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)
推荐答案
现在重写有一个方法叫做 discord.utils.get 您可以在其中实际获取具有特定参数的不和谐对象
Now in rewrite there's a method called discord.utils.get where you can actually getting discord objects with specific parameters
在您的情况下使用频道名称:
In your case with a channel name:
import discord channel = discord.utils.get(guild.text_channels, name="Name of channel")
如果 discord 找不到具有该名称的文本频道,则应为 None
Should be None if discord couldn't find a textchannel with that name