如何获取频道的名称,以便该机器人可以在其放置的任何服务器上运行,而无需更改代码?(在我放我在这里放什么"的代码中,我希望名称出现在变量中)谢谢
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 获取频道名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
python:不同包下同名的两个模块和类python: Two modules and classes with the same name under different packages(python:不同包下同名的两个模块和类)
配置 Python 以使用站点包的其他位置Configuring Python to use additional locations for site-packages(配置 Python 以使用站点包的其他位置)
如何在不重复导入顶级名称的情况下构造python包How to structure python packages without repeating top level name for import(如何在不重复导入顶级名称的情况下构造python包)
在 OpenShift 上安装 python 包Install python packages on OpenShift(在 OpenShift 上安装 python 包)
如何刷新 sys.path?How to refresh sys.path?(如何刷新 sys.path?)
分发带有已编译动态共享库的 Python 包Distribute a Python package with a compiled dynamic shared library(分发带有已编译动态共享库的 Python 包)