python - Discord.py 从非异步函数发送消息
问题描述
我有一个看起来像这样的程序:
import things
import discord
def on_thing_happen (variable):
get_info()
do thing()
# This is where I want to send a message in Discord
由于某些原因,我的其余代码无法在异步函数中工作。
有没有办法做到这一点?我不能使用异步 def。
解决方案
尝试这个:
import things
import discord
client = discord.Client()
def on_thing_happen (variable):
get_info()
do thing()
channel = client.get_channel(CHANNEL ID) #replace with channel id of text channel in discord
client.loop.create_task(channel.send('Message'))
所以取而代之的await channel.send('message')
是client.loop.create_task(channel.send('message')
我希望这行得通!
推荐阅读
- regex - 如何获得与特殊正则表达式匹配的行
- asp.net-core-mvc - ASP.NET Core 只为所有视图填充一次模型
- swift - 无法在选项卡视图控制器之间共享数据?
- android - 在 Acrobat Reader 中打开 PDF 文件
- angular - p-calendar angular 6 事件 onClick 不触发(和其他事件)
- python - 文件更改时Django页面不重新加载
- java - Java 抽象类的良好实践
- ios - Cocos2d-x 加速度计 xy 在 iPhone6 和 iPhoneX 中互换
- reactjs - 来自函数 JS 的 ApiFetch 传递变量
- perl - 从位置变量中去掉逗号