-
Notifications
You must be signed in to change notification settings - Fork 0
/
helperFunction.py
100 lines (87 loc) · 4.95 KB
/
helperFunction.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
import discord
import io
import textwrap
import re
from typing import Union, List, Set, Callable
from discord.ext import commands
async def splitAndSend(message:str,channel:discord.channel.TextChannel,removeMentions=False,sendAsEmbed=False):
async def genericSplit(text:str,threshold:int, splitFunction:Callable[[str],List[str]]) -> List[str]:
initialSegments = splitFunction(text)
finalSegments = list()
currentlyAssembledFinalSegment = ''
for segment in initialSegments:
if len(segment) > threshold:
if currentlyAssembledFinalSegment != '':
finalSegments.append(currentlyAssembledFinalSegment)
finalSegments.append(segment)
currentlyAssembledFinalSegment = ''
elif len(currentlyAssembledFinalSegment+segment) > threshold:
finalSegments.append(currentlyAssembledFinalSegment)
currentlyAssembledFinalSegment = segment
else:
currentlyAssembledFinalSegment += segment
finalSegments.append(currentlyAssembledFinalSegment)
return finalSegments
def safeGoodMessageAppend(message:str):
if message.strip() != '':
goodMessages.append(message)
MAXIMUM_LENGHT = 2000
if removeMentions:
message = discord.utils.escape_mentions(message)
if len(message) <= MAXIMUM_LENGHT:
await channel.send(message)
else:
goodMessages : List[str] = list()
lineSplitMesseges = await genericSplit(message,MAXIMUM_LENGHT,lambda x: x.splitlines(True))
for lineSplitMessage in lineSplitMesseges:
if len(lineSplitMessage) <= MAXIMUM_LENGHT:
safeGoodMessageAppend(lineSplitMessage)
else:
sentenceSplitMesseges = await genericSplit(lineSplitMessage,MAXIMUM_LENGHT,lambda x: re.split(r'((?<=[.?!])\s)', x))
for senteceSplitMessage in sentenceSplitMesseges:
if len(senteceSplitMessage) <= MAXIMUM_LENGHT:
safeGoodMessageAppend(senteceSplitMessage)
else:
for characterSplitMessage in textwrap.wrap(senteceSplitMessage,width=MAXIMUM_LENGHT,expand_tabs=False,replace_whitespace=False,drop_whitespace=False):
safeGoodMessageAppend(characterSplitMessage)
for goodMessage in goodMessages:
if sendAsEmbed:
await channel.send(embed=discord.Embed(description=goodMessage))
else:
await channel.send(goodMessage)
async def convertAttachementToFile(attachment:discord.Attachment) -> discord.File:
attachmentData = await attachment.read()
proccessedAttachmentData = io.BytesIO(attachmentData)
return discord.File(proccessedAttachmentData,filename=attachment.filename,spoiler=attachment.is_spoiler())
async def safeCopyMessagesToChannel(messages:List[discord.Message],channel:discord.TextChannel,removeMentions=False):
attachmentsWhichAreTooBig:List[discord.File] = list()
MAXIMUM_ATTACHMENT_SIZE : int = channel.guild.filesize_limit
for message in messages:
goodFiles : List[discord.Attachment] = list()
attachment:discord.Attachment
for attachment in message.attachments:
if attachment.size > MAXIMUM_ATTACHMENT_SIZE:
attachmentsWhichAreTooBig.append(attachment)
else:
goodFiles.append(await convertAttachementToFile(attachment))
if message.content != '' or len(goodFiles) > 0:
await channel.send(message.content,files=goodFiles)
if len(attachmentsWhichAreTooBig) > 0:
message = 'Some attachments were too large to send; please save them somewhere, lest they be lost when the links expire:\n'
for attachment in attachmentsWhichAreTooBig:
message += attachment.proxy_url+'\n'
await splitAndSend(message,channel,removeMentions)
#Returns all of a user's messages in a given channel (sorted from oldest to newest [using 'created_at', like you would do if you scrolled through a channel]).
async def getUserMessagesInChannel(channel:discord.TextChannel,user:Union[discord.Member,discord.User]) -> List[discord.Message]:
messages = list()
async for message in channel.history(limit=None,oldest_first=True):
if message.author == user:
messages.append(message)
return messages
#A check decorator to check whether the author has a role in the privileged-role list or has adminastrator rights.
def privilegedCheck():
async def predicate(ctx:commands.context.Context):
configuration = ctx.bot.get_cog('Configuration')
privileged_roles = await configuration.getPrivilegedRoles(ctx.guild)
return not privileged_roles.isdisjoint(ctx.author.roles) or ctx.author.guild_permissions.administrator
return commands.check(predicate)