As the title says When the URL is long, when the preview is displayed at the YouTube URL, the above message will not be visible to people using smartphones. I made this BOT because I thought it was inconvenient. Made in python
↑ BOT that when the URL is included in the message like this, it will be shortened if the reaction of the lower chain is pressed
Result ↓
import discord
import re
import asyncio
from urllib.parse import urlparse
#It's for this TOKEN test, so you can use it separately.
TOKEN = "NzA2MzQwNTMwMDE5MjM3OTQ0.Xq405w.pwRREjj-8N4MKph3QcV9NGb5EIM"
client = discord.Client()
@client.event
async def on_ready():
print("Ready")
@client.event
async def on_message(message):
if message.author.bot:
return
pattern = "https?://[\w/:%#\$&\?\(\)~\.=\+\-]+"
url_list = re.findall(pattern, message.content)
if url_list != []:
await message.add_reaction("⛓")
return
@client.event
async def on_reaction_add(reaction, user):
channel = client.get_channel(reaction.message.channel.id)
if reaction.count >= 2 and reaction.emoji == "⛓":
if reaction.count >= 3:
pass
else:
s = reaction.message.content
pattern = "https?://[\w/:%#\$&\?\(\)~\.=\+\-]+"
url_list = re.findall(pattern, s)
url_list[0]
mes = s.strip(url_list[0])
parsed_url = urlparse(url_list[0])
site = '{uri.scheme}://{uri.netloc}/'.format(uri=urlparse(url_list[0]))
embed = discord.Embed(title=f"{mes}",
description=f"[Short URL]({url_list[0]})",
color=0x87cefa)
embed.set_author(name=reaction.message.author.display_name,
icon_url=reaction.message.author.avatar_url_as(format="png"))
embed.set_footer(text=f"{site}")
await channel.send(embed=embed)
return
client.run(TOKEN)
** First one **
@client.event
async def on_message(message):
if message.author.bot:
return
#This ↑ this ↓
pattern = "https?://[\w/:%#\$&\?\(\)~\.=\+\-]+"
url_list = re.findall(pattern, message.content)
if url_list != []: #Whether the list is empty
await message.add_reaction("⛓")
return
This is the place that is called when a message comes, and the favorite is from this ↑ this ↓. The program from this ↑ this ↓ puts the URL included in the message in the list. If the list is empty, add a reaction. The URL guy http://trelab.info/python/python-%E6%AD%A3%E8%A6%8F%E8%A1%A8%E7%8F%BE%E3%81%A7url%E3%81%AE%E4%B8%80%E8%87%B4%E3%83%81%E3%82%A7%E3%83%83%E3%82%AF%E3%80%81%E6%8A%BD%E5%87%BA%E3%82%92%E8%A1%8C%E3%81%86/ Refer to ... No, I'm sorry.
The second
@client.event
async def on_reaction_add(reaction, user):
channel = client.get_channel(reaction.message.channel.id)
if reaction.count >= 2 and reaction.emoji == "⛓":
if reaction.count >= 3:
pass
else:
s = reaction.message.content
#This ↑ this ↓
pattern = "https?://[\w/:%#\$&\?\(\)~\.=\+\-]+"
url_list = re.findall(pattern, s)
url_list[0]
mes = s.strip(url_list[0])
parsed_url = urlparse(url_list[0])
site = '{uri.scheme}://{uri.netloc}/'.format(uri=urlparse(url_list[0]))
embed = discord.Embed(title=f"{mes}",
description=f"[Short URL]({url_list[0]})",
color=0x87cefa)
embed.set_author(name=reaction.message.author.display_name,
icon_url=reaction.message.author.avatar_url_as(format="png"))
embed.set_footer(text=f"{site}")
await channel.send(embed=embed)
return
Where there is a program to send a shortened URL. Called when a reaction is attached. The favorite is from the place where this ↑ this ↓ is written Before that, the one who determines whether the reaction is a chain After this ↑ this ↓
pattern = "https?://[\w/:%#\$&\?\(\)~\.=\+\-]+"
url_list = re.findall(pattern, s)
url_list[0]
#This ↑ this ↓
mes = s.strip(url_list[0])#Take the URL from the message
parsed_url = urlparse(url_list[0])#Remove the extra from the URL
site = '{uri.scheme}://{uri.netloc}/'.format(uri=urlparse(url_list[0]))
Is almost the same as the one I did in the first one After this ↑ this ↓, the URL is taken from the message and the extra one is removed from the URL. The one who removes the extra from the URL https://www.python.ambitious-engineer.com/archives/35 Refer to ...
embed = discord.Embed(title=f"{mes}",
description=f"[Short URL]({url_list[0]})",
color=0x87cefa)
embed.set_author(name=reaction.message.author.display_name,
icon_url=reaction.message.author.avatar_url_as(format="png"))
embed.set_footer(text=f"{site}")
await channel.send(embed=embed)
This is the one that sends the shortened one with embed
This completes
Hmm can not think of
the end
Recommended Posts