New Module: LyricCop
It will monitor selected channels for links and move them to the 'links' channel
This commit is contained in:
108
modules/LinkCop/cog.py
Normal file
108
modules/LinkCop/cog.py
Normal file
@@ -0,0 +1,108 @@
|
||||
import re
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
import random
|
||||
|
||||
regex_link = re.compile('(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)')
|
||||
|
||||
|
||||
### Langvars for the response strings
|
||||
### {%<langvar>%}
|
||||
### Possible langvars
|
||||
### 'mtn_user' - Mentions the user that sent the original message - {%mtn_user%}
|
||||
### 'ref_og_channel' - Reference the channel that the user sent the original message - {%ref_og_channel%}
|
||||
### 'ref_first_link' - Reference the first link that the user originally sent - {%ref_first_link%}
|
||||
### 'ref_og_msg' - Reference the original message that the user sent - {%ref_og_msg%}
|
||||
|
||||
random_message = ["{%mtn_user%}, tsk tsk. Links belong here:\n{%ref_og_msg%}", "{%mtn_user%}, the channel is quite literally called 'links':\n{%ref_og_msg%}",
|
||||
"{%mtn_user%}. Well, well, well, if it isn't the man who's been posting links in the wrong channel.\n'{%ref_og_msg%}'",
|
||||
"{%mtn_user%}, isn't this convenient. A whole channel for links and you put links in, and you put {%ref_first_link_%} in {%ref_og_channel%}.\n\n'{%ref_og_msg%}'",
|
||||
]
|
||||
|
||||
class LinkCop(commands.Cog):
|
||||
def __init__(self, bot): # , mention_group='Superadmins', channel_id=757379843792044102):
|
||||
self.bot = bot
|
||||
self.reply_channel_id = 918029426397184000 # 767303243285790721
|
||||
|
||||
self.blocked_channels = [
|
||||
918029426397184000,
|
||||
767303243285790721
|
||||
]
|
||||
|
||||
self.add_events()
|
||||
|
||||
def add_events(self):
|
||||
@self.bot.event
|
||||
async def on_message(ctx):
|
||||
### TODO check if user is an admin
|
||||
|
||||
if not ctx.author.id == self.bot.user.id:
|
||||
if ctx.channel.id in self.blocked_channels:
|
||||
if check_message(ctx.content):
|
||||
print('Msg with links detected in a blocked channel')
|
||||
|
||||
response_text = self.generate_response(ctx)
|
||||
|
||||
# Send the response in the links channel
|
||||
await self.send_message(response_text)
|
||||
# Delete the original message
|
||||
await ctx.delete()
|
||||
|
||||
async def send_message(self, message):
|
||||
send_channel = self.bot.get_channel(id=self.reply_channel_id)
|
||||
await send_channel.send(message)
|
||||
|
||||
def generate_response(self, ctx):
|
||||
# Get message
|
||||
og_message_text = ctx.content
|
||||
|
||||
# Get the random string to edit
|
||||
response_text = random_message[random.randint(0, len(random_message)-1)]
|
||||
|
||||
# Get the sender of the message
|
||||
member = ctx.author.id
|
||||
mtn_member = f"<@{member}>"
|
||||
|
||||
# Get the name of the channel the message was sent
|
||||
ref_og_channel = ctx.channel.name
|
||||
|
||||
# Get the first link the user sent
|
||||
ref_first_link = get_links(message_text=og_message_text)
|
||||
if ref_first_link:
|
||||
ref_first_link = ref_first_link[0]
|
||||
|
||||
# Mention the user
|
||||
response_text = str(response_text).replace("{%mtn_user%}", mtn_member)
|
||||
|
||||
# Reference the original channel
|
||||
response_text = str(response_text).replace("{%ref_og_channel%}", ref_og_channel)
|
||||
|
||||
# Reference the original message
|
||||
response_text = str(response_text).replace("{%ref_og_msg%}", og_message_text)
|
||||
|
||||
# Reference the first link
|
||||
response_text = str(response_text).replace("{%ref_first_link%}", ref_first_link)
|
||||
|
||||
return response_text
|
||||
|
||||
|
||||
def check_message(message_text):
|
||||
matches = regex_link.findall(message_text)
|
||||
|
||||
if bool(matches):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def get_links(message_text):
|
||||
links = regex_link.findall(message_text)
|
||||
|
||||
print(links)
|
||||
|
||||
if len(links) > 0:
|
||||
return links
|
||||
|
||||
|
||||
def setup(bot: commands.Bot):
|
||||
bot.add_cog(LinkCop(bot))
|
||||
Reference in New Issue
Block a user