gpt4 book ai didi

python - 使用discord.py获取 channel 的名称

转载 作者:行者123 更新时间:2023-12-03 15:16:52 24 4
gpt4 key购买 nike

如何获得 channel 名称,以便该机器人可以在其所安装的任何服务器上工作而无需更改代码? (在我放置“我在这里放什么”的代码中,是我希望名称位于变量中的位置)

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的方法,您可以在其中实际获取具有特定参数的不和谐对象
对于您的 channel 名称:

import discord
channel = discord.utils.get(guild.text_channels, name="Name of channel")
如果discord无法找到具有该名称的文本 channel ,则应为None

关于python - 使用discord.py获取 channel 的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52916317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com