college-bot-v1/cogs/events.py

192 lines
9 KiB
Python
Raw Normal View History

2022-12-28 21:26:56 +00:00
import random
2022-12-31 17:13:40 +00:00
from pathlib import Path
from typing import Optional, Tuple
2022-10-13 08:53:45 +01:00
from datetime import datetime, time
2022-10-06 09:20:23 +01:00
import discord
2022-10-13 08:53:45 +01:00
from discord.ext import commands, tasks
2022-12-07 13:45:58 +00:00
from utils import Student, get_or_none, console
2022-10-13 08:53:45 +01:00
from config import guilds, lupupa_warning
2022-10-06 09:20:23 +01:00
LTR = "\N{black rightwards arrow}\U0000fe0f"
RTL = "\N{leftwards black arrow}\U0000fe0f"
class Events(commands.Cog):
def __init__(self, bot):
self.bot = bot
2022-10-13 08:53:45 +01:00
self.lupupa_warning_task.start()
# noinspection DuplicatedCode
async def analyse_text(self, text: str) -> Optional[Tuple[float, float, float, float]]:
"""Analyse text for positivity, negativity and neutrality."""
def inner():
try:
from utils.sentiment_analysis import intensity_analyser
except ImportError:
return None
scores = intensity_analyser.polarity_scores(text)
return scores["pos"], scores["neu"], scores["neg"], scores["compound"]
async with self.bot.training_lock:
return await self.bot.loop.run_in_executor(None, inner)
2022-10-13 08:53:45 +01:00
def cog_unload(self):
self.lupupa_warning_task.stop()
@tasks.loop(minutes=30)
2022-10-13 08:53:45 +01:00
async def lupupa_warning_task(self):
2022-10-20 10:05:50 +01:00
if not self.bot.is_ready():
await self.bot.wait_until_ready()
2022-10-20 15:54:27 +01:00
now = datetime.now()
lupupa_warning_text = "\N{warning sign} Lupupa warning!!!"
2022-10-20 15:54:27 +01:00
lupupa_recovery_text = "\N{loudly crying face} Lupupa recovery..."
if lupupa_warning and now.strftime("%A") == "Thursday":
if now.time() > time(15, 15):
text = lupupa_recovery_text
2022-10-20 15:55:31 +01:00
status = discord.Status.idle
2022-10-20 15:54:27 +01:00
else:
text = lupupa_warning_text
2022-10-24 11:49:11 +01:00
status = discord.Status.dnd
2022-10-20 10:08:14 +01:00
if self.bot.activity:
2022-10-20 15:54:27 +01:00
if self.bot.activity.name == text:
2022-10-20 10:08:14 +01:00
return
await self.bot.change_presence(
2022-10-30 16:31:38 +00:00
activity=discord.Activity(name=text, type=discord.ActivityType.playing), status=status
2022-10-20 10:08:14 +01:00
)
else:
await self.bot.change_presence()
2022-10-06 09:20:23 +01:00
2022-10-06 09:34:23 +01:00
@commands.Cog.listener("on_raw_reaction_add")
2022-10-06 09:20:23 +01:00
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent):
channel: Optional[discord.TextChannel] = self.bot.get_channel(payload.channel_id)
2022-10-06 09:31:20 +01:00
if channel is not None:
2022-10-06 09:20:23 +01:00
try:
message: discord.Message = await channel.fetch_message(payload.message_id)
except discord.HTTPException:
return
2022-10-06 09:32:38 +01:00
if message.author.id == self.bot.user.id:
2022-10-09 19:27:02 +01:00
if payload.emoji.name == "\N{wastebasket}\U0000fe0f":
2022-10-06 09:31:20 +01:00
await message.delete(delay=1)
2022-10-06 09:20:23 +01:00
@commands.Cog.listener()
async def on_member_join(self, member: discord.Member):
if member.guild is None or member.guild.id not in guilds:
return
student: Optional[Student] = await get_or_none(Student, user_id=member.id)
if student and student.id:
role = discord.utils.find(lambda r: r.name.lower() == "verified", member.guild.roles)
if role and role < member.guild.me.top_role:
await member.add_roles(role, reason="Verified")
channel: discord.TextChannel = discord.utils.get(member.guild.text_channels, name="general")
if channel and channel.can_send():
await channel.send(
f"{LTR} {member.mention} (`{member}`, {f'{student.id}' if student else 'pending verification'})"
)
2022-10-06 09:20:23 +01:00
@commands.Cog.listener()
async def on_member_remove(self, member: discord.Member):
if member.guild is None or member.guild.id not in guilds:
return
student: Optional[Student] = await get_or_none(Student, user_id=member.id)
channel: discord.TextChannel = discord.utils.get(member.guild.text_channels, name="general")
if channel and channel.can_send():
await channel.send(
f"{RTL} {member.mention} (`{member}`, {f'{student.id}' if student else 'pending verification'})"
)
2022-10-06 09:20:23 +01:00
2022-10-09 19:27:02 +01:00
@commands.Cog.listener()
async def on_message(self, message: discord.Message):
2022-10-30 16:05:54 +00:00
if not message.guild:
2022-12-18 14:05:48 +00:00
return console.log("Ignoring, was not in a guild.")
2022-10-09 19:27:02 +01:00
if message.channel.name == "pinboard":
2022-10-10 18:15:48 +01:00
if message.type == discord.MessageType.pins_add:
await message.delete(delay=0.01)
else:
try:
await message.pin(reason="Automatic pinboard pinning")
except discord.HTTPException as e:
return await message.reply(f"Failed to auto-pin: {e}", delete_after=10)
elif message.channel.name in ("verify", "timetable") and message.author != self.bot.user:
if message.channel.permissions_for(message.guild.me).manage_messages:
await message.delete(delay=1)
2022-10-09 19:27:02 +01:00
2022-11-10 15:54:40 +00:00
else:
2022-11-15 21:45:39 +00:00
if message.author.bot is True:
2022-11-10 15:54:40 +00:00
return
if message.content:
if message.channel.can_send():
if "linux" in message.content.lower() and self.bot.user in message.mentions:
2022-12-28 21:26:56 +00:00
console.log(f"Responding to {message.author} with linux copypasta")
try:
with open("./copypasta.txt", "r") as f:
await message.reply(f.read())
except FileNotFoundError:
await message.reply(
"I'd just like to interject for a moment. What you're referring to as Linux, "
"is in fact, uh... I don't know, I forgot."
)
2022-12-31 17:13:40 +00:00
if "carat" in message.content.lower():
file = discord.File(Path(__file__).parent.parent / "carat.jpg")
await message.reply(file=file)
if message.channel.permissions_for(message.guild.me).add_reactions:
if "mpreg" in message.content.lower() or "\U0001fac3" in message.content.lower():
try:
await message.add_reaction("\U0001fac3")
except discord.HTTPException as e:
console.log("Failed to add mpreg reaction:", e)
if "lupupa" in message.content.lower():
try:
await message.add_reaction("\U0001fac3")
except discord.HTTPException as e:
console.log("Failed to add mpreg reaction:", e)
2022-12-28 21:26:56 +00:00
is_naus = random.randint(1, 100) == 32
if self.bot.user in message.mentions or message.channel.id == 1032974266527907901 or is_naus:
T_EMOJI = "\U0001f3f3\U0000fe0f\U0000200d\U000026a7\U0000fe0f"
G_EMOJI = "\U0001f3f3\U0000fe0f\U0000200d\U0001f308"
N_EMOJI = "\U0001f922"
C_EMOJI = "\U0000271d\U0000fe0f"
2022-12-28 21:26:56 +00:00
if any((x in message.content.lower() for x in ("trans", T_EMOJI, "femboy"))) or is_naus:
try:
await message.add_reaction(N_EMOJI)
except discord.HTTPException as e:
console.log("Failed to add trans reaction:", e)
if "gay" in message.content.lower() or G_EMOJI in message.content.lower():
try:
await message.add_reaction(C_EMOJI)
except discord.HTTPException as e:
console.log("Failed to add gay reaction:", e)
2022-12-18 14:05:48 +00:00
else:
console.log("No content.")
if self.bot.user in message.mentions:
if message.content.startswith(self.bot.user.mention):
if message.content.lower().endswith("bot"):
pos, neut, neg, _ = await self.analyse_text(message.content)
if pos > neg:
embed = discord.Embed(description=":D", color=discord.Color.green())
embed.set_footer(
text=f"Pos: {pos*100:.2f}% | Neutral: {neut*100:.2f}% | Neg: {neg*100:.2f}%"
)
elif pos == neg:
embed = discord.Embed(description=":|", color=discord.Color.greyple())
embed.set_footer(
text=f"Pos: {pos * 100:.2f}% | Neutral: {neut * 100:.2f}% | Neg: {neg * 100:.2f}%"
)
else:
embed = discord.Embed(description=":(", color=discord.Color.red())
embed.set_footer(
text=f"Pos: {pos*100:.2f}% | Neutral: {neut*100:.2f}% | Neg: {neg*100:.2f}%"
)
return await message.reply(embed=embed)
2022-11-10 15:54:40 +00:00
2022-10-06 09:20:23 +01:00
def setup(bot):
bot.add_cog(Events(bot))