forked from galacticwarrior9/IslamBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
203 lines (162 loc) · 4.78 KB
/
utils.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
201
202
203
import aiohttp
import configparser
from bs4 import BeautifulSoup
from discord import Embed
from typing import Union
import discord
import pandas as pd
config = configparser.ConfigParser()
config.read('config.ini')
def make_embed(**kwargs):
"""
Creates an embed message with specified inputs.
Parameters
----------
author
author_url
author_icon
user
colour
fields
inline
thumbnail
image
footer
footer_icon
"""
# Get the attributes from the user
Empty = Embed.Empty
if True:
# Get the author/title information
author = kwargs.get('author', Empty)
author_url = kwargs.get('author_url', Empty)
author_icon = kwargs.get('author_icon', Empty)
# Get the colour
colour = kwargs.get('colour', 0)
# Get the values
fields = kwargs.get('fields', {})
inline = kwargs.get('inline', True)
description = kwargs.get('description', Empty)
# Footer
footer = kwargs.get('footer', Empty)
footer_icon = kwargs.get('footer_icon', Empty)
# Images
thumbnail = kwargs.get('thumbnail', False)
image = kwargs.get('image', False)
# Filter the colour into a usable form
if type(colour).__name__ == 'Message':
colour = colour.author.colour.value
elif type(colour).__name__ == 'Server':
colour = colour.me.colour.value
elif type(colour).__name__ == 'Member':
colour = colour.colour.value
# Create an embed object with the specified colour
embedObj = Embed(colour=colour)
# Set the normal attributes
if author != Empty:
embedObj.set_author(name=author, url=author_url, icon_url=author_icon)
embedObj.set_footer(text=footer, icon_url=footer_icon)
embedObj.description = description
# Set the attributes that have no default
if image:
embedObj.set_image(url=image)
if thumbnail:
embedObj.set_thumbnail(url=thumbnail)
# Set the fields
for i, o in fields.items():
p = inline
if type(o) in [tuple, list]:
p = o[1]
o = o[0]
embedObj.add_field(name=i, value=o, inline=p)
# Return to user
return embedObj
async def get_site_source(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
text = await resp.read()
return BeautifulSoup(text.decode('utf-8', 'ignore'), 'html5lib')
def convert_to_arabic_number(number_string):
dic = {
'0': '۰',
'1': '١',
'2': '٢',
'3': '۳',
'4': '٤',
'5': '٥',
'6': '٦',
'7': '٧',
'8': '٨',
'9': '۹',
':': ':'
}
return "".join([dic[char] for char in number_string])
def convert_from_arabic_number(number_string):
dic = {
'۹': '9',
'٨': '8',
'٧': '7',
'٦': '6',
'٥': '5',
'٤': '4',
'۳': '3',
'٢': '2',
'١': '1',
'۰': '0',
':': ':'
}
return "".join([dic[char] for char in number_string])
path = "prefixes.csv"
def make_csv():
df = pd.DataFrame(columns=['guildID', 'prefix', 'authorID'])
df.to_csv(path, index=False)
def get_csv():
try:
pd.read_csv(path)
except FileNotFoundError:
make_csv()
df = pd.read_csv(path)
return df
class PrefixHandler:
df = get_csv()
@classmethod
def save(cls):
cls.df.to_csv(path, index=False)
@classmethod
def add_prefix(cls, author: discord.Member, guild_id: int, prefix: str):
if guild_id not in cls.df.guildID.values:
new_row = {
"guildID": guild_id,
"prefix": prefix,
"authorID": author.id
}
cls.df = cls.df.append(new_row, ignore_index=True)
cls.save()
else:
guild_row = cls.df[cls.df.guildID == guild_id]
guild_row.prefix.values[0] = prefix
cls.df[cls.df.guildID == guild_id] = guild_row
cls.save()
@classmethod
def remove_prefix(cls,guild_id : int):
if guild_id not in cls.df.guildID.values:
pass
else:
cls.df = cls.df[cls.df.guildID != guild_id]
cls.save()
@classmethod
def get_prefix(cls, guild_id: int) -> Union[str, None]:
prefix = None
try:
guild_row = cls.df[cls.df.guildID == guild_id]
prefix = guild_row.prefix.values[0]
except:
pass
return prefix
@classmethod
def get_default_prefix(cls):
prefix = config['IslamBot']['default_prefix']
return prefix
@classmethod
def has_custom_prefix(cls, guild_id: int) -> bool:
return guild_id in cls.df.guildID.values