It seems convenient to be able to put out an image immediately like a LINE stamp. I made it because I thought
1 First, register the image
2 Enclose the name used when registering in! In the message to be sent. Then the image is sent.
To delete the registered image, do s! Del [name]
#coding=utf-8
import discord
import re
import os
import glob
client = discord.Client()
@client.event
async def on_ready():
print('Connected')
@client.event
async def on_message(message):
if message.author.bot:
return
word = re.compile("s!add (.+)").search(message.content)
if word:
wo = word.group(1)
await message.channel.send("setting up...")
picu = message.attachments[0].url
file = str(message.guild.id) + "/" + wo + ".txt"
try:
os.mkdir(f"./{str(message.guild.id)}")
except FileExistsError:
pass
with open(file, mode='w') as f:
f.write(str(picu))
await message.channel.send("Added a stamp.")
f.close()
return
wordd = re.compile("s!del (.+)").search(message.content)
if wordd:
woo = wordd.group(1)
file = str(message.guild.id) + "/" + woo + ".txt"
if (os.path.exists(file)):
await message.channel.send("Deleting...")
os.remove(file)
await message.channel.send("The stamp has been deleted.")
else:
await message.channel.send("The stamp was not found.")
return
pe = re.compile('s!list (.+)').search(message.content)
if pe:
casfo = glob.glob(str(message.guild.id) + "/*")
nopas = str(message.guild.id)
lis = []
for file in casfo: #File list
prii = file.lstrip(nopas)
pri = prii.rstrip(".txt")
pr = pri.rstrip("/")
p = pr.lstrip("\\")
lis.append(p)
pa = int(pe.group(1))
no = len(lis)
embed = discord.Embed(title="Stamp list", description=f"{str(pa)}Page page\n Number of stamps:{len(lis)}", color=0x4682b4)
c = 0
cc = 0
if pa > 1:
ccc = pa*10
c = ccc - 10
while True:
try:
naiy = lis[c]
except:
if pa == 1:
await message.channel.send("There is no stamp yet.")
return
await message.channel.send(f"{str(pa)}There is no page yet")
return
c = c + 1
embed.add_field(name=str(c), value=naiy)
if c == pa * 10:
await message.channel.send(embed=embed)
return
if c == len(lis):
break
await message.channel.send(embed=embed)
return
s = message.content
if s.count("!") == 2:
x = s.find("!")
y = s.find("!", x+2)
sta = s[x + 1 : y]
if sta == None:
return
pas = str(message.guild.id) + "/" + sta + ".txt"
if os.path.exists(pas) == False:
return
with open(pas) as f:
url = f.read()
f.close()
await message.channel.send(url)
return
print('connecting...')
#Because it is TOKEN for test, it can be leaked
client.run("NzA2MzQwNTMwMDE5MjM3OTQ0.Xq405w.pwRREjj-8N4MKph3QcV9NGb5EIM")
It's very long because I don't have the vocabulary (?) Of the chords.
When registering images, create a folder with the server ID and Just create a text file with the URL of the image in it with the name of the image. What happens when you send a stamp If there are two !, check the text between the two !. Then read the text file with the name of the text between! In the folder with that server ID. Send the URL of the image in it. Complete
Explanation of the most important thing in this program ↑ this ↓ The one who examines the text between the two!
s = message.content #Because it's long, put it in s
if s.count("!") == 2:
x = s.find("!")
y = s.find("!", x+2)
sta = s[x + 1 : y]
if sta == None:
return
pas = str(message.guild.id) + "/" + sta + ".txt"
if os.path.exists(pas) == False:
return
with open(pas) as f:
url = f.read()
f.close()
await message.channel.send(url)
return
In the message! The part to look up the two and the name surrounded by them.
How it works: 1 First, if s.count ("!") == 2:, check if there are two! 2 Find the number of first! And second! Locations with x = s.find ("1") and y = s.find ("!", X + 2). 3 Examine the characters in the area surrounded by! At sta = s [x + 1: y] 4 Make sure it's not empty with if sta == None: 5 pas = str(message.guild.id) + "/" + sta + ".txt" Is the path of the text file where the URL of the registered image is recorded 6 if os.path.exists(pas) == False: Check if the file with the path of 5 exists 7 with open(pas) as f: Read the contents of the text file with 5 paths and send it
I can't think of it the end
Recommended Posts