mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-21 10:17:59 -05:00
[V3] Report Tool (#1281)
* Okay, let's fix the issues here hopefully. * This is working now * Unfinished, and needs a lot of testing. * more work * working * minor thing to remove * improve i18n and usage feedback
This commit is contained in:
62
redbot/core/utils/antispam.py
Normal file
62
redbot/core/utils/antispam.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Tuple, List
|
||||
from collections import namedtuple
|
||||
|
||||
Interval = Tuple[timedelta, int]
|
||||
AntiSpamInterval = namedtuple('AntiSpamInterval', ['period', 'frequency'])
|
||||
|
||||
|
||||
class AntiSpam:
|
||||
"""
|
||||
Custom class which is more flexible than using discord.py's
|
||||
`commands.cooldown()`
|
||||
|
||||
Can be intialized with a custom set of intervals
|
||||
These should be provided as a list of tuples in the form
|
||||
(timedelta, quantity)
|
||||
|
||||
Where quantity represents the maximum amount of times
|
||||
something should be allowed in an interval.
|
||||
"""
|
||||
# TODO : Decorator interface for command check using `spammy`
|
||||
# with insertion of the antispam element into context
|
||||
# for manual stamping on succesful command completion
|
||||
|
||||
default_intervals = [
|
||||
(timedelta(seconds=5), 3),
|
||||
(timedelta(minutes=1), 5),
|
||||
(timedelta(hours=1), 10),
|
||||
(timedelta(days=1), 24)
|
||||
]
|
||||
|
||||
def __init__(self, intervals: List[Interval]):
|
||||
self.__event_timestamps = []
|
||||
_itvs = intervals if intervals else self.default_intervals
|
||||
self.__intervals = [
|
||||
AntiSpamInterval(*x) for x in _itvs
|
||||
]
|
||||
self.__discard_after = max([x.period for x in self.__intervals])
|
||||
|
||||
def __interval_check(self, interval: AntiSpamInterval):
|
||||
return len(
|
||||
[t for t in self.__event_timestamps
|
||||
if (t + interval.period) > datetime.utcnow()]
|
||||
) >= interval.frequency
|
||||
|
||||
@property
|
||||
def spammy(self):
|
||||
"""
|
||||
use this to check if any interval criteria are met
|
||||
"""
|
||||
return any(self.__interval_check(x) for x in self.__intervals)
|
||||
|
||||
def stamp(self):
|
||||
"""
|
||||
Use this to mark an event that counts against the intervals
|
||||
as happening now
|
||||
"""
|
||||
self.__event_timestamps.append(datetime.utcnow())
|
||||
self.__event_timestamps = [
|
||||
t for t in self.__event_timestamps
|
||||
if t + self.__discard_after > datetime.utcnow()
|
||||
]
|
||||
135
redbot/core/utils/tunnel.py
Normal file
135
redbot/core/utils/tunnel.py
Normal file
@@ -0,0 +1,135 @@
|
||||
import discord
|
||||
from datetime import datetime
|
||||
from redbot.core.utils.chat_formatting import pagify
|
||||
import io
|
||||
import sys
|
||||
|
||||
_instances = {}
|
||||
|
||||
|
||||
class TunnelMeta(type):
|
||||
"""
|
||||
lets prevent having multiple tunnels with the same
|
||||
places involved.
|
||||
"""
|
||||
|
||||
def __call__(cls, *args, **kwargs):
|
||||
lockout_tuple = (
|
||||
(kwargs.get('sender'), kwargs.get('origin')),
|
||||
kwargs.get('recipient')
|
||||
)
|
||||
if not (
|
||||
any(
|
||||
lockout_tuple[0] == x[0]
|
||||
for x in _instances.keys()
|
||||
) or any(
|
||||
lockout_tuple[1] == x[1]
|
||||
for x in _instances.keys()
|
||||
)
|
||||
):
|
||||
_instances[lockout_tuple] = super(
|
||||
TunnelMeta, cls).__call__(*args, **kwargs)
|
||||
return _instances[lockout_tuple]
|
||||
elif lockout_tuple in _instances:
|
||||
return _instances[lockout_tuple]
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
class Tunnel(metaclass=TunnelMeta):
|
||||
"""
|
||||
A tunnel interface for messages
|
||||
|
||||
This will return None on init if the destination
|
||||
or source + origin pair is already in use
|
||||
|
||||
You should close tunnels when done with them
|
||||
"""
|
||||
|
||||
def __init__(self, *,
|
||||
sender: discord.Member,
|
||||
origin: discord.TextChannel,
|
||||
recipient: discord.User):
|
||||
self.sender = sender
|
||||
self.origin = origin
|
||||
self.recipient = recipient
|
||||
self.last_interaction = datetime.utcnow()
|
||||
|
||||
def __del__(self):
|
||||
lockout_tuple = ((self.sender, self.origin), self.recipient)
|
||||
_instances.pop(lockout_tuple, None)
|
||||
|
||||
def close(self):
|
||||
self.__del__()
|
||||
|
||||
async def react_close(self, *, uid: int, message: str):
|
||||
send_to = self.origin if uid == self.sender.id else self.sender
|
||||
closer = next(filter(
|
||||
lambda x: x.id == uid, (self.sender, self.recipient)), None)
|
||||
await send_to.send(
|
||||
message.format(closer=closer)
|
||||
)
|
||||
self.close()
|
||||
|
||||
@property
|
||||
def members(self):
|
||||
return (self.sender, self.recipient)
|
||||
|
||||
@property
|
||||
def minutes_since(self):
|
||||
return (self.last_interaction - datetime.utcnow()).minutes
|
||||
|
||||
async def communicate(self, *,
|
||||
message: discord.Message,
|
||||
topic: str=None,
|
||||
skip_message_content: bool=False):
|
||||
if message.channel == self.origin \
|
||||
and message.author == self.sender:
|
||||
send_to = self.recipient
|
||||
elif message.author == self.recipient \
|
||||
and isinstance(message.channel, discord.DMChannel):
|
||||
send_to = self.origin
|
||||
else:
|
||||
return None
|
||||
|
||||
if not skip_message_content:
|
||||
content = "\n".join((topic, message.content)) if topic \
|
||||
else message.content
|
||||
else:
|
||||
content = topic
|
||||
|
||||
attach = None
|
||||
if message.attachments:
|
||||
files = []
|
||||
size = 0
|
||||
max_size = 8 * 1024 * 1024
|
||||
for a in message.attachments:
|
||||
_fp = io.BytesIO()
|
||||
await a.save(_fp)
|
||||
size += sys.getsizeof(_fp)
|
||||
if size > max_size:
|
||||
await send_to.send(
|
||||
"Could not forward attatchments. "
|
||||
"Total size of attachments in a single "
|
||||
"message must be less than 8MB."
|
||||
)
|
||||
break
|
||||
files.append(
|
||||
discord.File(_fp, filename=a.filename)
|
||||
)
|
||||
else:
|
||||
attach = files
|
||||
|
||||
rets = []
|
||||
for page in pagify(content):
|
||||
rets.append(
|
||||
await send_to.send(content, files=attach)
|
||||
)
|
||||
if attach:
|
||||
del attach
|
||||
|
||||
await message.add_reaction("\N{WHITE HEAVY CHECK MARK}")
|
||||
await message.add_reaction("\N{NEGATIVE SQUARED CROSS MARK}")
|
||||
self.last_interaction = datetime.utcnow()
|
||||
await rets[-1].add_reaction("\N{NEGATIVE SQUARED CROSS MARK}")
|
||||
return [rets[-1].id, message.id]
|
||||
Reference in New Issue
Block a user