【发布时间】:2022-08-06 03:49:28
【问题描述】:
如何创建一个函数(没有异步),每次它(函数)在代码中的某处执行时向特定通道发送消息?
def sendMsg():
channel = client.getChannel(Channel id)
message.channel.send(\"example message\")
#excecuting the function
sendMsg()
什么都不做
async def on_message():
await message.channel.send(\"example message\")
只有这个有效
所以我的问题是我是否可以修改顶部的代码使其工作?
-
请包括minimal reproducible example。不使用 asyncio 就无法通过 discordpy 发送消息。你能得到的最接近的是上课。您可能需要阅读 asyncio 的文档。
-
你说的那个“有效”不工作.
await是必需的。 -
@EricJin 抱歉,在我的工作代码中,我使用了等待。只是忘了写在例子里
-
你需要
async def send_msg,然后你可以在它前面使用await message.channel.send。message也没有在其中定义 - 你的意思是让它成为一个全局变量还是作为参数传递? -
>还没有定义消息。好吧,idk,但它有效
标签: python async-await discord discord.py