TotalFreedomBot/events.py

159 lines
6.9 KiB
Python
Raw Normal View History

2020-10-28 12:25:25 +00:00
import discord
2020-11-21 12:37:08 +00:00
import logscript
2020-10-28 12:25:25 +00:00
from datetime import datetime
from discord.ext import commands
from checks import *
from functions import *
2020-12-08 22:44:56 +00:00
from unicode import confirm, clipboard, cancel
2020-11-21 12:37:08 +00:00
from telnet import telnet
2020-10-30 13:57:14 +00:00
2020-10-31 20:08:36 +00:00
telnet_ip = "localhost"
telnet_port = 22
telnet_username = "root"
telnet_password = "root"
2020-11-21 12:37:08 +00:00
print = logscript.logging.getLogger().critical
2020-10-31 20:08:36 +00:00
2020-10-28 12:25:25 +00:00
class Events(commands.Cog):
def __init__(self, bot):
self.bot = bot
2020-10-30 13:57:14 +00:00
2020-10-28 12:25:25 +00:00
@commands.Cog.listener()
async def on_ready(self):
2020-10-31 20:08:36 +00:00
config = read_json('config')
telnet_ip = config['TELNET_IP']
telnet_port = config['TELNET_PORT']
telnet_username = config['TELNET_USERNAME']
telnet_password = config['TELNET_PASSWORD']
2020-12-02 21:43:03 +00:00
2020-10-28 12:25:25 +00:00
self.bot.reaction_roles = []
self.bot.telnet_object = telnet(
telnet_ip, telnet_port, telnet_username, telnet_password)
2020-10-31 02:04:01 +00:00
self.bot.telnet_object.connect()
2020-10-31 02:04:01 +00:00
print(f'[{str(datetime.utcnow().replace(microsecond=0))[11:]} INFO]: [TELNET] Bot logged into Telnet as: {self.bot.telnet_object.username}')
2020-10-31 20:39:17 +00:00
reaction_data = read_json('config')
self.bot.reaction_roles = reaction_data['reaction_roles']
2020-10-31 02:04:01 +00:00
print(f'[{str(datetime.utcnow().replace(microsecond=0))[11:]} INFO]: [Client] {self.bot.user.name} is online.')
2020-10-28 12:25:25 +00:00
game = discord.Game('play.totalfreedom.me')
await self.bot.change_presence(status=discord.Status.online, activity=game)
2020-10-28 12:25:25 +00:00
guildCount = len(self.bot.guilds)
2020-11-21 12:37:08 +00:00
print(f'[{str(datetime.utcnow().replace(microsecond=0))[11:]} INFO]: [Guilds] bot currently in {guildCount} guilds.')
2020-10-28 12:25:25 +00:00
for guild in self.bot.guilds:
2020-10-31 02:04:01 +00:00
print(f'[{str(datetime.utcnow().replace(microsecond=0))[11:]} INFO]: [Guilds] Connected to guild: {guild.name}, Owner: {guild.owner}')
2020-10-28 12:25:25 +00:00
@commands.Cog.listener()
async def on_message_edit(self, before, after):
if not isinstance(before.author, discord.Member):
return
2020-11-21 12:37:08 +00:00
if before.guild.id != guild_id:
2020-10-28 12:25:25 +00:00
return
users = removed_user_mentions(before.mentions, after.mentions)
roles = removed_role_mentions(
before.role_mentions, after.role_mentions)
2020-10-28 12:25:25 +00:00
if users:
users = ", ".join([str(member) for member in users])
if roles:
roles = ", ".join([role.name for role in roles])
if not users and not roles:
return
embed = discord.Embed(
description="In {}".format(before.channel.mention))
2020-10-28 12:25:25 +00:00
if users:
embed.add_field(name="Users", value=users, inline=True)
if roles:
embed.add_field(name="Roles", value=roles, inline=True)
embed.color = 0xFF0000
embed.title = "Message Edit"
embed.set_footer(text=str(before.author),
icon_url=get_avatar(before.author))
2020-10-28 12:25:25 +00:00
channel = before.guild.get_channel(mentions_channel_id)
await channel.send(embed=embed)
2020-10-28 12:25:25 +00:00
@commands.Cog.listener()
async def on_message_delete(self, message):
if not isinstance(message.author, discord.Member):
return
if message.guild.id != guild_id:
return
users = None
roles = None
if did_mention_other_user(message.mentions, message.author):
users = ", ".join([str(member) for member in message.mentions])
if message.role_mentions:
roles = ", ".join([role.name for role in message.role_mentions])
if not users and not roles:
return
embed = discord.Embed(
description="In {}".format(message.channel.mention))
2020-10-28 12:25:25 +00:00
if users is not None:
embed.add_field(name="Users", value=users, inline=True)
if roles is not None:
embed.add_field(name="Roles", value=roles, inline=True)
embed.color = 0xFF0000
embed.title = "Message Deletion"
embed.set_footer(text=str(message.author),
icon_url=get_avatar(message.author))
2020-10-28 12:25:25 +00:00
channel = message.guild.get_channel(mentions_channel_id)
await channel.send(embed=embed)
2020-10-28 12:25:25 +00:00
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
2020-11-21 12:37:08 +00:00
em = discord.Embed()
em.title = 'Command Error'
em.description = f'{error}'
em.colour = 0xFF0000
await ctx.send(embed=em)
print(f'[{str(datetime.utcnow().replace(microsecond=0))[11:]} INFO]: [Commands] {ctx.author} (ID: {ctx.author.id}) failed running: {ctx.message.content} in guild: {ctx.guild.name}')
2020-10-28 12:25:25 +00:00
@commands.Cog.listener()
async def on_command_completion(self, ctx):
2020-11-21 12:37:08 +00:00
print(f'[{str(datetime.utcnow().replace(microsecond=0))[11:]} INFO]: [Commands] {ctx.author} (ID: {ctx.author.id}) ran: {ctx.message.content} in guild: {ctx.guild.name}')
2020-10-30 13:57:14 +00:00
bot_logs_channel = self.bot.get_channel(bot_logs_channel_id)
log = discord.Embed(
title='Logging', description=f'{ctx.author} (ID: {ctx.author.id}) ran: {ctx.message.content}', colour=0xA84300)
2020-10-30 13:57:14 +00:00
await bot_logs_channel.send(embed=log)
2020-10-28 12:25:25 +00:00
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
if payload.member == self.bot.user:
pass
else:
for role_id, msg_id, emoji in self.bot.reaction_roles:
2020-10-31 20:39:17 +00:00
if msg_id == payload.message_id and emoji == str(payload.emoji.name):
2020-10-28 12:25:25 +00:00
await payload.member.add_roles(self.bot.get_guild(payload.guild_id).get_role(role_id), reason='reaction')
if payload.channel_id == reports_channel_id:
guild = self.bot.get_guild(guild_id)
reports_channel = self.bot.get_channel(reports_channel_id)
report = await reports_channel.fetch_message(payload.message_id)
if report.author == guild.me:
if payload.emoji.name == clipboard:
await report.add_reaction(confirm)
await report.add_reaction(cancel)
elif payload.emoji.name == cancel:
await report.clear_reactions()
await report.add_reaction(clipboard)
elif payload.emoji.name == confirm:
embed = report.embeds[0]
archived_reports_channel = self.bot.get_channel(
archived_reports_channel_id)
2020-10-28 12:25:25 +00:00
await report.delete()
await archived_reports_channel.send("Handled by " + guild.get_member(payload.user_id).mention, embed=embed)
2020-10-28 12:25:25 +00:00
@commands.Cog.listener()
async def on_raw_reaction_remove(self, payload):
if payload.member == self.bot.user:
pass
else:
for role_id, msg_id, emoji in self.bot.reaction_roles:
2020-10-31 20:39:17 +00:00
if msg_id == payload.message_id and emoji == str(payload.emoji.name):
2020-10-28 12:25:25 +00:00
await self.bot.get_guild(payload.guild_id).get_member(payload.user_id).remove_roles(self.bot.get_guild(payload.guild_id).get_role(role_id), reason='reaction')
2020-10-28 12:25:25 +00:00
def setup(bot):
bot.add_cog(Events(bot))