-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.py
123 lines (101 loc) · 4.3 KB
/
util.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
'''
Utility functions shared across cogs
'''
import os
from typing import Optional, Union
import discord
from discord.ext import commands
from PIL.Image import registered_extensions
import db
IGNORE_COMMAND_LIST = [
'purge', 'purgelast', 'buy', 'eight', 'eject', 'google', 'groupbuy',
'northfacing', 'oos', 'pins', 'spraylubing', 'vendors', 'fakelifealert',
'lifealert', 'trade', 'vote', 'flashsales', 'help', 'rk61', ''
]
MECHMARKET_SCRAPE_HEADERS = {
'authority': 'www.reddit.com',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,'
'image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'accept-language': 'en-US,en;q=0.9',
'cache-control': 'max-age=0',
'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/120.0.0.0 Safari/537.36',
}
def supported_image_extensions() -> set[str]:
'''
this takes a second to run as pillow inits, caching out of paranoia
'''
return set(registered_extensions().keys())
def is_image(uri: str) -> bool:
'''see if a URI directs to an image'''
possible_ext = os.path.splitext(uri)[1].lower()
try:
if possible_ext and possible_ext in supported_image_extensions():
return True
except KeyError:
pass
return False
def get_id_from_tag(tag: str) -> int:
'''Convert <@2823848929872937942> to a regular ID'''
return int(''.join([char for char in list(tag) if char.isdigit()]))
async def get_reply_message(message: discord.Message) -> discord.Message:
'''Find the message that bot will reply to later'''
if message.reference is not None and message.reference.message_id:
message = await message.channel.fetch_message(message.reference.message_id)
return message
# TODO: handle via role IDs
async def apply_role(member: discord.Member, user_id: int,
role_name: str, reason: Optional[str] = None,
enter_in_db: bool = True) -> None:
'''Apply a role to a member, and mark it in db'''
role = discord.utils.get(member.guild.roles, name=role_name)
if role:
await member.add_roles(role, reason=reason)
if enter_in_db:
with db.bot_db:
query = db.RoleAssignment.select().where(
(db.RoleAssignment.user_id == user_id) &
(db.RoleAssignment.role_name == role_name)
)
if not query.exists():
db.RoleAssignment.create(
user_id=user_id,
role_name=role_name
)
# TODO: handle via role IDs
async def remove_role(member: discord.Member, user_id: int,
role_name: str) -> None:
'''Remove a role from a member, and remove it from db'''
role = discord.utils.get(member.guild.roles, name=role_name)
await member.remove_roles(role) # type: ignore
with db.bot_db:
db.RoleAssignment.delete().where(
(db.RoleAssignment.user_id == user_id) &
(db.RoleAssignment.role_name == role_name)
).execute()
async def handle_error(ctx: commands.Context, error_message: Optional[str]):
'''send an error message to a user when they misuse a command'''
if error_message:
channel = await ctx.message.author.create_dm()
await channel.send(error_message)
await ctx.message.delete()
async def fetch_primary_guild(client: discord.Client):
'''get the guild the bot is supposed to be running on primarily'''
guild_id = int(os.getenv('SERVER_ID', '0'))
guild = await client.fetch_guild(guild_id, with_counts=False)
if guild:
return guild
def user_has_role_from_id(author: Union[discord.Member, discord.abc.User],
role_id: int) -> bool:
'''determine if message author has corresponding role ID'''
if hasattr(author, 'roles'):
return bool(discord.utils.get(author.roles, id=role_id))
return False