首页 > 解决方案 > 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。

标签: pythonasynchronousdiscord.py

解决方案


尝试这个:

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')

我希望这行得通!


推荐阅读