-
I'm making a Discord bot that listens for events from TeamTalk (a confrencing system) synchronously and sends them to a discord channel. The problem, however, is that the TeamTalk API is synchronous, and so I can't call async functions from it. Is there a simple way to send a single message to a Discord channel without using Async? I've tried create_task, but it just sits there without a traceback, and I'm honestly out of ideas. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I might suggest you use asyncio.to_thread to run any of your sync functions asynchronously in a separate thread. They can return a value or trigger a callback without being blocking. You can also wrap the to_thread call in an async func if you'd like to await the callback from within that directly (implementation of that specific part is up to you) |
Beta Was this translation helpful? Give feedback.
-
You can use webhooks to send messages to a specific channel w/o needing any other instrumentation. import discord
import requests
url = 'https://discord.com/api/webhooks/:id/:secret'
session = requests.Session()
webhook = discord.webhook.SyncWebhook.from_url(url, session=session)
webhook.send('Example Content') You can create a webhook by going into a channel's settings into the integrations tab, and then copying the url for use in your code. |
Beta Was this translation helpful? Give feedback.
-
Webhooks did what I wanted perfectly, thanks! |
Beta Was this translation helpful? Give feedback.
You can use webhooks to send messages to a specific channel w/o needing any other instrumentation.
The library includes a way to execute webhooks in an async and sync manner which would be useful for your use-case. See a short example:
You can create a webhook by going into a channel's settings into the integrations tab, and then copying the url for use in your code.