The content explained in this article is not particularly difficult, so it is a fairly simple explanation. Please note. We will use Webhooks this time, but I think that even people who are not familiar with how to use Webhooks can understand it. However, it does not explain how to create a webhook or how to get a link, so please see other articles.
・ Editor --Atom · Execution shell-PlatformIO IDE Terminal (Atom extension) | It's just a command prompt. · Python --Python 3.9.1 ・ Discord.py --discord.py 1.5.1.
import discord
from discord.ext import commands
import requests
import json
bot = commands.Bot(command_prefix='!')
TOKEN = ''
webhook_url = ''
main_content = {
"username": "Webhooks username",
"avatar_url": "https://1.bp.blogspot.com/-d3vDLBoPktU/WvQHWMBRhII/AAAAAAABL6E/Grg-XGzr9jEODAxkRcbqIXu-mFA9gTp3wCLcBGAs/s400/internet_404_page_not_found.png ",
"content": "Webhooks content",
"embeds": [
{
"title": "first embeds title",
"description": "first embeds description",
"color": 0x00ffff,
},
{
"title": "second embeds title",
"description": "second embeds description",
"color": 0xff0000,
}
]
}
@bot.event
async def on_ready():
print('ready')
@bot.command()
async def test(ctx):
requests.post(webhook_url, json.dumps(main_content), headers={'Content-Type': 'application/json'})
return
bot.run(TOKEN)
import discord
from discord.ext import commands
import requests
import json
From above,
・ Modules essential for developing Discord.py -Modules that are essential for using the command framework -Module for sending webhooks -Module used to recognize that part of the code is written in a json file
is.
TOKEN = ''
webhook_url = ''
main_content = {
"username": "Webhooks username",
"avatar_url": "https://1.bp.blogspot.com/-d3vDLBoPktU/WvQHWMBRhII/AAAAAAABL6E/Grg-XGzr9jEODAxkRcbqIXu-mFA9gTp3wCLcBGAs/s400/internet_404_page_not_found.png ",
"content": "Webhooks content",
"embeds": [
{
"title": "first embeds title",
"description": "first embeds description",
"color": 0x00ffff,
},
{
"title": "second embeds title",
"description": "second embeds description",
"color": 0xff0000,
}
]
}
I will not explain about TOKEN because it is a matter of course. (~~ I can't say it's just annoying ~~)
・ Webhook_url --URL of the webhook to use -Main_content --Information about the webhook to send
webhook_url
For the time being
ch_webhooks = await message.channel.webhooks()
webhook = discord.utils.get(ch_webhooks, name='test')
webhook_url = webhook.token
But you can get it.
main_content
I will explain in detail the main_content
variable, which can be said to be the most important in this code.
Depending on the person, it may be understandable at a glance, but for the time being.
json.dumps
function is used, I think that there is no problem with single quotes.
First, from the top three lines.username --The username to use when sending webhooks. avatar_url --The URL of the icon to use when sending a webhook. content --What to fill in the webhook.
And embeds
, which is an element for sending multiple Embeds.
By the way, if you only need to use Embed, you don't need the above three.
As you can see, embeds
is a plural, that is, a list (array) type. So there is a [
after the colon. (It was annoying to look up the official name)
What is different from this Embed definition method and the normal d.py Embed definition method is that, for example, title ='title'
was changed to"title": "title"
. It's just that. After that, there is a {}
to represent it as one unit, and that's it.
Example: ↓
discord.Embed(title='title', description='description', color=0x00ffff, url='url').set_author(name='author_name', url='author_url', icon_url='author_icon_url')
#But,
{
"embeds":[{
"title": "title",
"description": "description",
"color": 0x00ffff,
"url": "url",
"author":{
"name": "author_name",
"url": "author_url",
"icon_url": "author_icon_url"
}
}]
}
#Just become.
By the way, if you don't have to comply with PEP8, you don't have to think about indentation.
So, when using multiple Embeds, it looks like this
{
"embeds":[
{
"title": "title",
"description": "description",
"color": 0x00ffff,
"url": "url",
"author":{
"name": "author_name",
"url": "author_url",
"icon_url": "author_icon_url"
}
},
{
"title": "title",
"description": "description",
"color": 0x00ffff,
"url": "url",
"author":{
"name": "author_name",
"url": "author_url",
"icon_url": "author_icon_url"
}
]
}
Separate them with commas and incorporate two or more elements. Rumor has it that up to 10 Embeds can be embedded. I haven't tried it. If anyone has verified it, I would be grateful if you could let me know in the comments.
on_ready
@bot.event
async def on_ready():
print('ready')
Needless to say, all you have to do is check if the bot has started successfully. I don't need a detailed explanation.
@bot.command()
async def test(ctx):
requests.post(webhook_url, json.dumps(main_content), headers={'Content-Type': 'application/json'})
return
For the time being, the command test
is used here, but of course you can do other than that.
Now, let's take a look inside requests.post
.
webhook_url --URL of the webhook defined (acquired) above main_content --The one that is packed with the information of the Webhook message defined above (
json.dumps ()
is the one to convert to json format. Required.) headers-I don't really understand this either. However, it will not be sent successfully without this. The one that should be written for the time being.
Yes, the explanation is over. It was easy, wasn't it?
Use discord webhook in python requests post
Recommended Posts