When it comes to making a discord bot using python, it seems that there are roughly two types.
--Types that work interactively ――It seems that a library called discord.py is convenient. Not covered in this article. --One-sided notification type ――Click here to notify regularly. This is explained below.
You need to get it for each channel. You can refer to "Get Webhook URL" on this page. Option settings can be made with code, so you can pass it.
-[Ruby] Post a message to Discord with a webhook
The simplest configuration is as follows.
import requests, json
webhook_url = 'Webhook URL you just got'
main_content = {'content': 'Text to send'}
headers = {'Content-Type': 'application/json'}
response = requests.post(webhook_url, json.dumps(main_content), headers=headers)
Successful transmission!
The look of the bot can be defined in the code as follows: It seems that the URL of the image is required to set the icon. Below, the URL of the twitter account icon is given. In the case of a local image file, is there no choice but to set it when getting the Webhook URL?
import requests, json
webhook_url = 'Webhook URL you just got'
main_content = {
'username': 'name',
'avatar_url': 'Image URL',
'content': 'text'
}
headers = {'Content-Type': 'application/json'}
response = requests.post(webhook_url, json.dumps(main_content), headers=headers)
Successful transmission!
Use embeds. For more information, click here (https://birdie0.github.io/discord-webhooks-guide/structure/embeds.html). An example is given below.
import requests, json
webhook_url = 'Webhook URL you just got'
embeds = [
{
'description': 'google page',
'color': 15146762,
'image': {
'url': 'Image URL'
}
}
]
main_content = {
'username': 'name',
'avatar_url': 'Image URL',
'content': 'text',
'embeds': embeds
}
headers = {'Content-Type': 'application/json'}
response = requests.post(webhook_url, json.dumps(main_content), headers=headers)
It looks like this.
** The color of the left vertical bar is set with ** color
** in ** ʻembeds` ** **.
The correspondence between numbers and colors is here. Move the "Color Mixer" bar to find the color you want to display and give it the number to the right of "Decimal:".
For example, it looks like this.
When sending only text, give arguments only for channel
and content
.
If you want to use embedding, give the required information with ʻemb`.
emb = {
'description': 'Embedded text',
'color': 'color',
'img_url': 'Image URL',
'content': 'Text'
}
def send_discord_msg(channel, content, emb=0):
webhook_dic = {'channel 1': 'webhook URL for channel 1',
'channel 2': 'webhook URL for channel 2'}
webhook_url = webhook_dic[channel]
main_content = {
'username': 'bot name',
'avatar_url': 'Icon URL',
'content': content
}
if emb != 0:
color_dic = {
'Color 1': 15146762,
'Color 2': 49356,
}
embeds = [
{
'description': emb['description'],
"color": color_dic[emb['color']],
"image": {
"url": emb['img_url']
},
}
]
main_content.update({'embeds': embeds})
main_content['content'] = emb['content']
headers = {'Content-Type': 'application/json'}
try:
res = requests.post(webhook_url, json.dumps(main_content), headers=headers)
except Exception as e:
print(e)
Recommended Posts