【发布时间】:2022-01-04 21:13:41
【问题描述】:
这是我的代码的骨架版本:
bot.py
import discord
client = discord.Client()
async def send_notification(notification):#this method is tested and works when called from same file
for guild in client.guilds:
for channel in guild.text_channels:
if channel.name == CHANNEL_NAME:
await channel.sent(notification)
def start_bot():
client.run(TOKEN)
def notification(notification):
asyncio.create_task(send_notification(notification))#likely error here
main.py
import bot
from time import sleep
def main():
bot.start_bot()
sleep(10)
bot.notification('some notification')
main()
您好,我正在尝试从不同的 python 文件向所有行会发送消息。我知道我在处理异步任务的方式上犯了一些基本错误。目前 main.py 甚至没有到达 sleep() 语句。
- 有没有办法从 main.py 引用 client 以便可以使用它调用方法
- 我可以创建一些 api 以便任何 python 文件都可以访问机器人内部的方法
提前致谢。
【问题讨论】:
标签: python asynchronous discord.py