Discord.py 从非异步函数发送消息

sy1*_*vi3 1 python asynchronous 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
Run Code Online (Sandbox Code Playgroud)

由于某些原因,我的其余代码无法在异步函数中工作。

有什么办法可以做到这一点吗?我无法使用异步定义。

小智 8

尝试这个:

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'))
Run Code Online (Sandbox Code Playgroud)

所以取而代之的await channel.send('message')client.loop.create_task(channel.send('message')

我希望这能起作用!