This repository has been archived by the owner on Nov 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTagCog.py
97 lines (76 loc) · 4.21 KB
/
TagCog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import asyncio
import discord
from discord import Member
from discord.ext import commands
from fuzzywuzzy import process
from Data import TagConfig, RolesConfig
class TagCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def tag(self, ctx, *args):
query = ''
for arg in args:
query = query + ' ' + arg
# trimming the whitespace at the start and end of the arg string
query = query.lstrip().rstrip()
if not query.startswith("create") and not query.startswith("delete") and not query.startswith("list"):
async with ctx.channel.typing():
str2Match = query
strOptions = TagConfig().get_all_tags()
# Getting a list of tuples that includes the tag name and the match percentage
highest = process.extract(str2Match, strOptions)
# Finding the three best matches and sorting them
threeBest = sorted(highest, reverse=True, key=lambda x: x[1])[:3]
if threeBest[0][1] > 80:
match = threeBest[0]
await self.send_formatted_tag(str(match[0]), TagConfig().get_tag(TagConfig().get_path_of_tag(str(match[0]))), ctx.channel)
else:
await ctx.send("Nicht sicher, was du gemeint hast. Ist dein Tag hier dabei?\n\n`" + str(threeBest[0][0]) + "` (" + str(threeBest[0][1]) + "%)\n`" + str(threeBest[1][0]) + "` (" + str(threeBest[1][1]) + "%) \n`" + str(threeBest[2][0]) + "` (" + str(threeBest[2][1]) + "%)")
else:
member = ctx.message.author
# Check if member has a higher role than 'Moderator'
# if member.top_role > ctx.guild.get_role(int(RolesConfig().get_roles()['Moderator'])):
if isinstance(member, Member):
if member.top_role > ctx.guild.get_role(int(TagConfig().get_min_role())):
if query.startswith("create"):
def check(m):
return m.author == ctx.author
await ctx.channel.send("Erstelle Tag '" + query.split(' ')[1] + "'. Bitte gib den Text in __einer Nachricht__ an!")
try:
text = await self.bot.wait_for('message', check=check, timeout=30)
except asyncio.TimeoutError:
await ctx.channel.send("Breche ab, Timeout erreicht!")
else:
self.create_tag(query.split(' ')[1:], text.content, ctx.message.author.name)
await ctx.channel.send("Tag erstellt!")
# print(query)
elif query.startswith("delete"):
pass
elif query.startswith("list"):
await ctx.channel.send("Folgende Tags sind verfügbar:\n\n" + '\n'.join(TagConfig().get_all_tags()))
else:
await ctx.send("Du hast leider nicht die nötige Berechtigung, um diesen Command auszuführen.")
else:
await ctx.send("Dieser Command kann nur auf einem Discord Server ausgeführt werden!")
async def send_formatted_tag(self, tag_name, tag_text: str, channel):
if tag_text.startswith('@'):
author = tag_text.split('\n')[0][1:]
embed = discord.Embed()
embed.add_field(name=tag_name, value="by " + author, inline=False)
await channel.send(embed=embed)
await channel.send('\n'.join(tag_text.split('\n')[1:]))
else:
author = "System"
embed = discord.Embed()
embed.add_field(name=tag_name, value="by " + author, inline=False)
await channel.send(embed=embed)
await channel.send('\n'.join(tag_text.split('\n')[1:]))
def create_tag(self, tag_name, tag_text, author):
if isinstance(tag_name, list):
tag_name = '_'.join(tag_name)
file = open("tags/" + tag_name + ".txt", "w+", encoding='utf-8')
file.write("@" + author + "\n")
file.write(tag_text)
file.close()
TagConfig().add_tag_to_config("tags/" + tag_name + ".txt", tag_name)