-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsponseredby.py
184 lines (141 loc) · 7.62 KB
/
sponseredby.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
import discord
from discord import slash_command, Option
from discord.ext import commands
import os
from typing import List, Dict, Optional
from extra import utils
from mysqldb import the_database
guild_ids: List[int] = [int(os.getenv("SERVER_ID", 123))]
sponsored_by_category_id: int = int(os.getenv("SPONSORED_BY_CATEGORY_ID", 123))
class SponsoredBy(commands.Cog):
""" A category for managing the Sponsored by category
and its features. """
ping_cache: Dict[int, float] = {}
def __init__(self, client: commands.Bot) -> None:
""" The class init method. """
self.client = client
# Events
@commands.Cog.listener()
async def on_ready(self) -> None:
""" Tells when the cog is ready to go. """
print("The SponsoredBy cog is ready!")
# Commands
@slash_command(name="ping_role", guild_ids=guild_ids)
@commands.has_permissions(administrator=True)
async def ping_role(self, ctx: discord.ApplicationContext,
role: Option(discord.Role, name="role_to_ping", description="Select the role to ping.", required=True)
) -> None:
""" Pings a role in the 'SPONSERED BY:' category. """
if not ctx.channel.category or ctx.channel.category.id != sponsored_by_category_id:
return await ctx.respond(f"**You can only use this command in __<#{sponsored_by_category_id}>__ category!**", ephemeral=True)
member_ts = self.ping_cache.get(ctx.author.id)
time_now = await utils.get_timestamp()
if member_ts:
sub = time_now - member_ts
if sub <= 300:
return await ctx.respond(
f"**You are on cooldown to apply, try again in {(300-sub)/60:.1f} minutes**", ephemeral=True)
if not await self.get_user_ping_permission(ctx.author.id):
return await ctx.respond(f"**You don't have permission to ping here with this command!**", ephemeral=True)
# self.ping_cache[ctx.author.id] = time_now
role_ping = role.mention if role.name[0] != "@" else role
await ctx.respond(content=role_ping, allowed_mentions=discord.AllowedMentions.all())
@slash_command(name="add_ping_permission", guild_ids=guild_ids)
@commands.has_permissions(administrator=True)
async def add_ping_permission(self, ctx: discord.ApplicationContext,
user: Option(discord.Member, description="The person to give the permission to.", required=True)
) -> None:
""" Adds permission for a user to ping everyone
in the 'SPONSORED BY:' category. """
user_has_ping_perms = await self.get_user_ping_permission(user.id)
if user_has_ping_perms:
return await ctx.respond(f"**This user already has permission to ping in the __<#{sponsored_by_category_id}>__ category!**", ephemeral=True)
await self.insert_user_ping_permission(user.id)
await ctx.respond(f"**Now {user.mention} can ping __everyone__ in the __<#{sponsored_by_category_id}>__ category!**", ephemeral=True)
@slash_command(name="delete_ping_permission", guild_ids=guild_ids)
@commands.has_permissions(administrator=True)
async def delete_ping_permission(self, ctx: discord.ApplicationContext,
user: Option(discord.Member, description="The person to remove the permission from.", required=True)
) -> None:
""" Removes permission from a user to ping everyone
in the 'SPONSORED BY:' category. """
user_has_ping_perms = await self.get_user_ping_permission(user.id)
if not user_has_ping_perms:
return await ctx.respond(f"**This user doesn't even have permission to ping in the __<#{sponsored_by_category_id}>__!**", ephemeral=True)
await self.delete_user_ping_permission(user.id)
await ctx.respond(f"**Now {user.mention} can no longer ping __everyone__ in the __<#{sponsored_by_category_id}>__ category!**", ephemeral=True)
# Database
@commands.has_permissions(administrator=True)
@commands.command(hidden=True)
async def create_table_user_ping_permission(self, ctx) -> None:
""" (ADM) Creates the UserPingPermission table. """
await ctx.message.delete()
if await self.table_user_ping_permission():
return await ctx.send("**Table __UserPingPermission__ already exists!**")
mycursor, db = await the_database()
await mycursor.execute("CREATE TABLE UserPingPermission (user_id BIGINT NOT NULL, PRIMARY KEY(user_id));")
await db.commit()
await mycursor.close()
return await ctx.send("**Table __UserPingPermission__ created!**", delete_after=5)
@commands.has_permissions(administrator=True)
@commands.command(hidden=True)
async def drop_table_user_ping_permission(self, ctx) -> None:
""" (ADM) Drops the UserPingPermission table. """
await ctx.message.delete()
if not await self.table_user_ping_permission():
return await ctx.send("**Table __UserPingPermission__ doesn't exist!**")
mycursor, db = await the_database()
await mycursor.execute("DROP TABLE UserPingPermission;")
await db.commit()
await mycursor.close()
return await ctx.send("**Table __UserPingPermission__ dropped!**", delete_after=5)
@commands.has_permissions(administrator=True)
@commands.command(hidden=True)
async def reset_table_user_ping_permission(self, ctx) -> None:
""" (ADM) Resets the UserPingPermission table. """
await ctx.message.delete()
if not await self.table_user_ping_permission():
return await ctx.send("**Table __UserPingPermission__ doesn't exist yet!**")
mycursor, db = await the_database()
await mycursor.execute("DELETE FROM UserPingPermission;")
await db.commit()
await mycursor.close()
return await ctx.send("**Table __UserPingPermission__ reset!**", delete_after=5)
async def table_user_ping_permission(self) -> bool:
""" Checks whether the UserPingPermission table exists. """
mycursor, _ = await the_database()
await mycursor.execute("SHOW TABLE STATUS LIKE 'UserPingPermission';")
exists = await mycursor.fetchone()
await mycursor.close()
if exists:
return True
else:
return False
async def insert_user_ping_permission(self, user_id: int) -> None:
""" Inserts a row into the UserPingPermission table for
a specific user.
:param user_id: The ID of the user to insert it for. """
mycursor, db = await the_database()
await mycursor.execute("INSERT INTO UserPingPermission (user_id) VALUES (%s);", (user_id,))
await db.commit()
await mycursor.close()
async def get_user_ping_permission(self, user_id: int) -> Optional[Dict[str, int]]:
""" Gets the data from the UserPingPermission for
a specific user.
:param user_id: The ID of the user to fetch. """
mycursor, _ = await the_database()
await mycursor.execute("SELECT * FROM UserPingPermission WHERE user_id = %s;", (user_id,))
user_ping_permission = await mycursor.fetchone()
await mycursor.close()
return user_ping_permission
async def delete_user_ping_permission(self, user_id: int) -> None:
""" Deletes a row from the UserPingPermission table for
a specific user.
:param user_id: The ID of the user to remove it for. """
mycursor, db = await the_database()
await mycursor.execute("DELETE FROM UserPingPermission WHERE user_id = %s;", (user_id,))
await db.commit()
await mycursor.close()
def setup(client: commands.Bot) -> None:
""" The cog's setup function. """
client.add_cog(SponsoredBy(client))