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 获取频道名称
基础教程推荐
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
