sy1vi3
sy1vi3

Reputation: 181

Discord.py send message from non-async function

I have a program that looks like this:

import things
import discord


def on_thing_happen (variable):
    get_info()
    do thing()
    # This is where I want to send a message in Discord

For reasons, the rest of my code cannot work in an async function.

Is there any way to do this? I cannot use an async def.

Upvotes: 1

Views: 3706

Answers (1)

Aadit Bansal
Aadit Bansal

Reputation: 51

Try this:

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

So instead of await channel.send('message') it is client.loop.create_task(channel.send('message')

I hope this works!

Upvotes: 8

Related Questions