1
Fork 0
mirror of https://github.com/allthingslinux/tux.git synced 2024-10-02 16:43:12 +00:00

fix: Linting and formatting via Ruff

This commit is contained in:
rm-rf-tux 2024-09-21 22:17:27 +00:00 committed by github-actions[bot]
parent c07c086f3b
commit 68b7f4de7f

View file

@ -1,20 +1,18 @@
from time import time
from collections import defaultdict
import asyncio
from typing import DefaultDict
from tux.utils.constants import CONST
from tux.bot import Tux
from collections import defaultdict
from time import time
import discord
from discord.ext import tasks, commands
from discord.ext import commands, tasks
from loguru import logger
from tux.bot import Tux
from tux.utils.constants import CONST
# Helper function required as YAML keys are str. Channel and user IDs are int.
def convert_dict_str_to_int(original_dict: dict[str, int]) -> dict[int, int]:
converted_dict:dict[int, int] = {}
converted_dict: dict[int, int] = {}
for key, value in original_dict.items():
try:
@ -25,26 +23,31 @@ def convert_dict_str_to_int(original_dict: dict[str, int]) -> dict[int, int]:
return converted_dict
class GifLimiter(commands.Cog):
class GifLimiter(commands.Cog):
"""
This class is a handler for GIF ratelimiting.
It keeps a list of GIF send times and routinely removes old times.
If a user posts a GIF, the message_handler function should be externally called.
It will delete the message if the user, channel or server-wide quota is exceeded.
"""
def __init__ (self, bot: Tux) -> None:
def __init__(self, bot: Tux) -> None:
self.bot = bot
# Read config options and save them to local variables to avoid excessive reads
# From the CONST[] dictionary
self.recent_gif_age: int = CONST.RECENT_GIF_AGE # Max age for a GIF to be considered a recent post
self.channelwide_gif_limits: dict[int, int] = convert_dict_str_to_int(CONST.GIF_LIMITS_CHANNEL) # Max GIFs sent recently for specific channels
self.user_gif_limits: dict[int, int] = convert_dict_str_to_int(CONST.GIF_LIMITS) # Max recent GIFs sent by a user to be able to send a GIF in a channel
self.gif_limit_exclude: list[int] = CONST.GIF_LIMIT_EXCLUDE # list of channels in which not to count GIFs
self.recent_gif_age: int = CONST.RECENT_GIF_AGE # Max age for a GIF to be considered a recent post
self.channelwide_gif_limits: dict[int, int] = convert_dict_str_to_int(
CONST.GIF_LIMITS_CHANNEL
) # Max GIFs sent recently for specific channels
self.user_gif_limits: dict[int, int] = convert_dict_str_to_int(
CONST.GIF_LIMITS
) # Max recent GIFs sent by a user to be able to send a GIF in a channel
self.gif_limit_exclude: list[int] = CONST.GIF_LIMIT_EXCLUDE # list of channels in which not to count GIFs
# Timestamps for recently-sent GIFs for the server, and channels
self.recent_gifs_by_user: defaultdict[int, list[int]] = defaultdict(list) # UID, list of timestamps
self.recent_gifs_by_channel: defaultdict[int, list[int]] = defaultdict(list) # Channel ID, list of timestamps
self.recent_gifs_by_user: defaultdict[int, list[int]] = defaultdict(list) # UID, list of timestamps
self.recent_gifs_by_channel: defaultdict[int, list[int]] = defaultdict(list) # Channel ID, list of timestamps
self.recent_gifs_serverwide: list[int] = []
# Deletes the message passed as an argument, and creates a self-deleting message explaining the reason
@ -57,28 +60,31 @@ class GifLimiter(commands.Cog):
@commands.Cog.listener()
async def on_message(self, message: discord.Message) -> None:
# Nothing to do if the message doesn't have a .gif embed, or if it was sent in a blacklisted channel
if (len(message.embeds) == 0
or not "gif" in message.content.lower()
or message.channel.id in self.gif_limit_exclude):
if (
len(message.embeds) == 0
or "gif" not in message.content.lower()
or message.channel.id in self.gif_limit_exclude
):
return
channel: int = message.channel.id
user: int = message.author.id
# Check if the message infringes on any ratelimits
if (channel in self.channelwide_gif_limits
if (
channel in self.channelwide_gif_limits
and channel in self.recent_gifs_by_channel
and len(self.recent_gifs_by_channel[channel]) >= self.channelwide_gif_limits[channel]):
and len(self.recent_gifs_by_channel[channel]) >= self.channelwide_gif_limits[channel]
):
await self.delete_message(message, "for channel")
return
if (user in self.recent_gifs_by_user
if (
user in self.recent_gifs_by_user
and channel in self.user_gif_limits
and len(self.recent_gifs_by_user[user]) >= self.user_gif_limits[channel]):
and len(self.recent_gifs_by_user[user]) >= self.user_gif_limits[channel]
):
await self.delete_message(message, "for user")
return
@ -93,21 +99,15 @@ class GifLimiter(commands.Cog):
current_time: float = time()
for channel_id, timestamps in self.recent_gifs_by_channel.items():
self.recent_gifs_by_channel[channel_id] = [
t for t in timestamps
if current_time - t < self.recent_gif_age
]
self.recent_gifs_by_channel[channel_id] = [t for t in timestamps if current_time - t < self.recent_gif_age]
for user_id, timestamps in self.recent_gifs_by_user.items():
self.recent_gifs_by_user[user_id] = [
t for t in timestamps
if current_time - t < self.recent_gif_age
]
self.recent_gifs_by_user[user_id] = [t for t in timestamps if current_time - t < self.recent_gif_age]
# Delete user key if no GIF has recently been sent by them
if len(self.recent_gifs_by_user[user_id]) == 0:
del self.recent_gifs_by_user[user_id]
async def setup(bot: Tux) -> None:
await bot.add_cog(GifLimiter(bot))