Python 3.x 不运行的不协调Python Bot
我是discord.py的新手,想了解一些特性。我如何在不运行bot的情况下执行函数?你看,我想从其他上下文调用一些bot函数。我不喜欢我的机器人读取事件或类似的SMT,例如,只要在我的程序调用时创建一个语音频道。我怎样才能实现这样的功能?也许我应该考虑为discord API编写自定义处理程序,例如,使用请求库?您的机器人必须登录discord才能使用API执行操作 我建议您以不同的线程运行bot,并从主线程运行coro。 像这样: 这是您的bot所在的文件:Python 3.x 不运行的不协调Python Bot,python-3.x,discord.py,Python 3.x,Discord.py,我是discord.py的新手,想了解一些特性。我如何在不运行bot的情况下执行函数?你看,我想从其他上下文调用一些bot函数。我不喜欢我的机器人读取事件或类似的SMT,例如,只要在我的程序调用时创建一个语音频道。我怎样才能实现这样的功能?也许我应该考虑为discord API编写自定义处理程序,例如,使用请求库?您的机器人必须登录discord才能使用API执行操作 我建议您以不同的线程运行bot,并从主线程运行coro。 像这样: 这是您的bot所在的文件: from discord.ext
from discord.ext.commands import Bot
import threading
import discord
client = Bot(command_prefix="!")
@client.event
async def on_ready():
print("Ready")
async def create_voice(guild_id):
await client.wait_until_ready() # Waits for bot is logged in and the cache is loaded
guild = client.get_guild(guild_id)
await guild.create_voice_channel("Hallo")
print("Done")
thread = threading.Thread(target=client.run, args=["Token"])
thread.start()
主文件:
import bot_run
import asyncio
loop = asyncio.get_event_loop()
asyncio.run_coroutine_threadsafe(bot_run.create_voice(guild_id), loop)
希望这有帮助:D非常感谢。我在考虑线程外解决方案,但据我所知,它在discord中不可用。py@ftelnov这对你有用吗?我自己试过,效果很好。我正在使用Python3.7和discord.py 1.5.1。我不确定你的评论是否对你不起作用^^。如果我错了,请回答。是的,对我来说效果很好。我只是对线程的额外资源感到有点难过:)