Issue
I am designing an app where I can send notification to my discord channel when something happen with my python code (e.g new user signup on my website). It will be a one way communication as only python app will send message to discord channel.
Here is what I have tried.
import os
import discord
import asyncio
TOKEN = ""
GUILD = ""
def sendMessage(message):
client = discord.Client()
@client.event
async def on_ready():
channel = client.get_channel(706554288985473048)
await channel.send(message)
print("done")
return ""
client.run(TOKEN)
print("can you see me?")
if __name__ == '__main__':
sendMessage("abc")
sendMessage("def")
The issue is only first message is being sent (i-e abc) and then aysn function is blocking the second call (def).
I don't need to listen to discord events and I don't need to keep the network communication open. Is there any way where I can just post the text (post method of api like we use normally) to discord server without listening to events?
Thanks.
Solution
You can use a Discord webhook.
First, make a webhook in the Discord channel you'd like to send messages to. Here's the Discord help page for that.
Then, use the discord.Webhook.from_url
method to fetch a Webhook
object from the URL Discord gave you.
Finally, use the discord.Webhook.send
method to send a message using the webhook.
Here's an example using the requests
module:
import requests
from discord import Webhook, RequestsWebhookAdapter
webhook = Webhook.from_url("url-here", adapter=RequestsWebhookAdapter())
webhook.send("Hello World")
In discord.py v2, the syntax has changed a bit
import requests
from discord import SyncWebhook
webhook = SyncWebhook.from_url("url-here")
webhook.send("Hello World")
Answered By - Ben Soyka
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.