get the name of a channel using discord.py(使用 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)
推荐答案
现在重写有一个方法叫做 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
这篇关于使用 discord.py 获取频道名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 discord.py 获取频道名称
基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01