-
Notifications
You must be signed in to change notification settings - Fork 3
/
urban.py
200 lines (175 loc) · 7.12 KB
/
urban.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import re
import aiohttp
import asyncio
import discord
import dateutil.parser as dparser
from datetime import datetime
from urllib import parse as url_parse
from discord.ext import commands
from core import basecog
from core.text import text
from core.config import config
class UrbanItem:
__slots__ = ["word", "definition", "example", "permalink", "author", "written_on"]
def __init__(
self,
word: str = None,
definition: str = None,
example: str = None,
permalink: str = None,
author: str = None,
written_on: datetime = None,
):
self.word = word
self.definition = definition
self.example = example
self.permalink = permalink
self.author = author
self.written_on = written_on
def __repr__(self):
return (
f'<UrbanItem word="{self.word}" definition="{self.definition}" '
f'example="{self.example}" permalink="{self.permalink}" '
f'author="{self.author}" written_on="{self.written_on}">'
)
class Urban(basecog.Basecog):
"""Urbandictionary search"""
def __init__(self, bot):
super().__init__(bot)
def urban_embeds(self, ctx, urban_list):
embed_list = []
for idx, item in enumerate(urban_list):
if len(item.definition) > 1024:
item.definition = item.definition[0:1021] + "`…`"
if len(item.example) > 1024:
item.example = item.example[0:1021] + "`…`"
embed = self.create_embed(
author=ctx.message.author,
title=item.word,
url=item.permalink,
timestamp=item.written_on,
)
if item.definition != "":
embed.add_field(name="Definition", value=item.definition, inline=False)
if item.example != "":
embed.add_field(name="Example", value=item.example, inline=False)
embed.add_field(
name="Page",
value="{curr}/{total}".format(curr=idx + 1, total=len(urban_list)),
inline=False,
)
embed_list.append(embed)
return embed_list
async def urban_pages(self, ctx, embeds):
message = await ctx.send(embed=embeds[0])
pagenum = 0
await message.add_reaction("◀️")
await message.add_reaction("▶️")
while True:
def check(reaction, user):
return (
reaction.message.id == message.id
and (str(reaction.emoji) == "◀️" or str(reaction.emoji) == "▶️")
and user == ctx.message.author
)
try:
reaction, user = await self.bot.wait_for(
"reaction_add", check=check, timeout=300.0
)
except asyncio.TimeoutError:
await message.clear_reactions()
break
else:
if str(reaction.emoji) == "◀️":
pagenum -= 1
if pagenum < 0:
pagenum = len(embeds) - 1
try:
await message.remove_reaction("◀️", user)
except discord.errors.Forbidden:
pass
await message.edit(embed=embeds[pagenum])
if str(reaction.emoji) == "▶️":
pagenum += 1
if pagenum >= len(embeds):
pagenum = 0
try:
await message.remove_reaction("▶️", user)
except discord.errors.Forbidden:
pass
await message.edit(embed=embeds[pagenum])
@commands.command(
brief=text.get("urban", "urban_desc"),
description=text.get("urban", "urban_desc"),
help=text.fill("urban", "urban_help", prefix=config.prefix),
)
async def urban(self, ctx):
await self.deleteCommand(ctx, now=True)
message = ctx.message
args = message.content.split(" ")
if len(args) == 1:
await ctx.send(
">>> " + text.fill("urban", "urban_help", prefix=config.prefix)
)
return
args.pop(0)
search = " ".join(args)
term = url_parse.quote(search)
async with ctx.typing():
try:
async with aiohttp.ClientSession(raise_for_status=True) as session:
async with session.get(
f"http://api.urbandictionary.com/v0/define?term={term}"
) as response:
json_response = await response.json()
lis = json_response["list"]
except aiohttp.ClientResponseError as e:
embed = self.create_embed(
author=ctx.message.author,
title="Critical error:",
color=config.color_error,
)
embed.add_field(
name="API replied with:",
value=f"`{e.status} {e.message}`"
"\n*This could mean UrbanDictionary is experiencing an outage, a network connection error has occured, or you provided a wrong request.*",
inline=False,
)
await ctx.send(embed=embed)
return
else:
# Request was successful
urban_list = []
for item in lis:
regex = re.compile(
r"^(?:http|ftp)s?://" # http:// or https://
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|" # domain...
r"localhost|" # localhost...
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" # ...or ip
r"(?::\d+)?" # optional port
r"(?:/?|[/?]\S+)$",
re.IGNORECASE,
) # Checks for valid URL This exact regex is used by Django (https://github.com/django/django/blob/stable/1.3.x/django/core/validators.py#L45)
match_result = re.match(regex, item["permalink"])
permalink = (
match_result.group(0) if match_result is not None else None
)
urban_item = UrbanItem(
item["word"],
re.sub(r"[!\[]|[!\]]", "**", item["definition"]),
re.sub(r"[!\[]|[!\]]", "**", item["example"]),
permalink,
item["author"],
dparser.parse(item["written_on"]),
)
urban_list.append(urban_item)
if urban_list != []:
embeds = self.urban_embeds(ctx, urban_list)
pass
else:
await ctx.send("No results found.")
return
await self.urban_pages(ctx, embeds)
return
def setup(bot):
bot.add_cog(Urban(bot))