Skip to content

Commit b8cc6f4

Browse files
authored
Merge pull request #43 from brandons209/update-3.5
Update 3.5
2 parents 4988906 + 93af1a8 commit b8cc6f4

File tree

1 file changed

+85
-3
lines changed

1 file changed

+85
-3
lines changed

pony/pony.py

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from redbot.core.utils.chat_formatting import *
33
from redbot.core import Config, checks, commands
44
from urllib import parse
5-
from typing import Literal, Optional
5+
from typing import Literal, Optional, Union
66
import aiohttp
77
import os
88
import traceback
@@ -12,6 +12,8 @@
1212

1313

1414
class Pony(commands.Cog):
15+
__version__ = "5.0.0"
16+
1517
def __init__(self, bot):
1618
super().__init__()
1719
self.bot = bot
@@ -23,9 +25,11 @@ def __init__(self, bot):
2325
"display_artist": False,
2426
"cooldown": 10,
2527
}
28+
default_channel = {"filters": []}
2629
self.cooldowns = {}
2730
self.config.register_guild(**self.default_guild)
2831
self.config.register_global(**default_global)
32+
self.config.register_channel(**default_channel)
2933

3034
self.task = asyncio.create_task(self.init())
3135

@@ -127,6 +131,83 @@ async def _list_ponyfilter(self, ctx):
127131
target_guild = "Default"
128132
await ctx.send("{} pony filter list contains:```\n{}```".format(target_guild, filter_list))
129133

134+
@ponyfilter.group(name="channel")
135+
async def ponyfilter_channel(self, ctx: commands.Context):
136+
"""
137+
Manage channel override filters
138+
139+
If filters are a set for a channel they **completely** override server level filters.
140+
"""
141+
pass
142+
143+
@ponyfilter_channel.command(name="add")
144+
async def channel_add_ponyfilter(
145+
self,
146+
ctx: commands.Context,
147+
channel: Union[discord.TextChannel, discord.VoiceChannel, discord.Thread],
148+
filter_tag: str,
149+
):
150+
"""Adds a tag to the channel's pony filter list
151+
152+
Example: [p]ponyfilter channel #channel add safe"""
153+
filters = await self.config.channel(channel).filters()
154+
max_filters = await self.config.maxfilters()
155+
# if reached limit of max filters, don't add
156+
if len(filters) < max_filters:
157+
if filter_tag not in filters:
158+
async with self.config.channel(channel).filters() as old_filter:
159+
old_filter.append(filter_tag)
160+
await ctx.send("Filter '{}' added to the {}'s pony filter list.".format(filter_tag, channel.mention))
161+
else:
162+
await ctx.send(
163+
"Filter '{}' is already in the {}'s pony filter list.".format(filter_tag, channel.mention)
164+
)
165+
else:
166+
await ctx.send("This channel has exceeded the maximum filters ({}/{}).".format(len(filters), max_filters))
167+
168+
@ponyfilter_channel.command(name="del")
169+
async def channel_del_ponyfilter(
170+
self,
171+
ctx,
172+
channel: Union[discord.TextChannel, discord.VoiceChannel, discord.Thread],
173+
filter_tag: str = "",
174+
):
175+
"""Deletes a tag from the channel's pony filter list
176+
177+
Without arguments, clears the channel's filters
178+
179+
Example: [p]ponyfilter channel #channel del safe"""
180+
filters = await self.config.channel(channel).filters()
181+
if len(filter_tag) > 0:
182+
if filter_tag in filters:
183+
async with self.config.channel(channel).filters() as old_filter:
184+
old_filter.remove(filter_tag)
185+
await ctx.send(
186+
"Filter '{}' deleted from the {}'s pony filter list.".format(filter_tag, channel.mention)
187+
)
188+
else:
189+
await ctx.send(
190+
"Filter '{}' does not exist in the {}'s pony filter list.".format(filter_tag, channel.mention)
191+
)
192+
else:
193+
await self.config.channel(channel).filters.clear()
194+
await ctx.send("Cleared {}'s filters.".format(channel.mention))
195+
196+
@ponyfilter_channel.command(name="list")
197+
async def channel_list_ponyfilter(
198+
self,
199+
ctx: commands.Context,
200+
channel: Union[discord.TextChannel, discord.VoiceChannel, discord.Thread],
201+
):
202+
"""Lists all of the filters currently applied to the current server"""
203+
filters = await self.config.channel(channel).filters()
204+
target = "{}'s".format(channel.name)
205+
if filters:
206+
filter_list = "\n".join(sorted(filters))
207+
else:
208+
filter_list = "***No Filters Set***"
209+
await ctx.send("{} pony filter list contains:```\n{}```".format(target, filter_list))
210+
130211
@commands.group()
131212
@checks.admin()
132213
async def ponyset(self, ctx):
@@ -193,7 +274,6 @@ async def _maxfilters_ponyset(self, ctx, new_max_filters: Optional[int] = None):
193274
await ctx.send("Current filter limit: {} filters.".format(max_filters))
194275
return
195276

196-
guild = ctx.guild
197277
await self.config.maxfilters.set(new_max_filters)
198278
await ctx.send("Maximum filters allowed per server for pony set to '{}'.".format(new_max_filters))
199279

@@ -255,7 +335,7 @@ async def _import_ponyset(self, ctx, path_to_import):
255335

256336
async def fetch_image(self, ctx, randomize: bool = False, tags: str = "", mascot=False):
257337
guild = ctx.guild
258-
338+
channel = ctx.channel
259339
# check cooldown
260340
if self.cooldowns[guild.id].get(ctx.author.id, 0) > time.time():
261341
left = self.cooldowns[guild.id].get(ctx.author.id, 0) - time.time()
@@ -268,6 +348,8 @@ async def fetch_image(self, ctx, randomize: bool = False, tags: str = "", mascot
268348

269349
tags = [t for t in tags.split(",") if t != ""]
270350
filters = await self.config.guild(guild).filters()
351+
channel_filters = await self.config.channel(channel).filters()
352+
filters = filters if len(channel_filters) == 0 else channel_filters
271353
verbose = await self.config.guild(guild).verbose()
272354
display_artist = await self.config.guild(guild).display_artist()
273355

0 commit comments

Comments
 (0)