Browse Source

Logging des messages refait avec possibilité d'assigner un channel

master
l4p1n 1 year ago
parent
commit
6ab8e6cd0f
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS. GPG Key ID: 8A2C2F68B58DC35C
4 changed files with 144 additions and 118 deletions
  1. +97
    -111
      cmds/logging.py
  2. +21
    -0
      db/migrations/20190720150250_remake_logging_table.sql
  3. +7
    -0
      db/migrations/20190720151608_add_logging_channel_id_to_config.sql
  4. +19
    -7
      db/schema.sql

+ 97
- 111
cmds/logging.py View File

@@ -1,4 +1,5 @@
import discord
import logging

from lib.EventTypes import Events
from lib.config_manager import ConfigManager
@@ -9,161 +10,146 @@ from discord.ext import commands
class Logging(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.colors = {
"edited": 0xff9900,
"deleted": 0xe91616,
"created": 0xccff66
}

@commands.Cog.listener()
async def on_message(self, message: discord.Message):
if not await self._wants_logging(message.guild.id):
return
self.logging = logging.getLogger('discord-bot.logging')

await self._log_action(
discord_message_id=message.id,
discord_user_id=message.author.id,
discord_channel_id=message.channel.id,
discord_server_id=message.guild.id,
discord_user_name=str(message.author),
discord_channel_name=message.channel.name,
discord_server_name=message.guild.name,
event_type=Events.CREATE_MESSAGE,
message=message.content,
is_channel_nsfw=message.channel.nsfw,
is_user_bot=message.author.bot,
discord_created_at=message.created_at
)
# @commands.Cog.listener()
# async def on_message(self, message: discord.Message):
# if not await self._wants_logging(message.guild.id):
# return

@commands.Cog.listener()
async def on_message_edit(self, before: discord.Message, after: discord.Message):
if not await self._wants_logging(after.guild.id):
# Sauver en base et envoyer si nécessaire
if after.author.bot:
return

await self._log_action(
discord_message_id=after.id,
discord_user_id=after.author.id,
discord_channel_id=after.channel.id,
discord_server_id=after.guild.id,
discord_user_name=str(after.author),
discord_channel_name=after.channel.name,
discord_server_name=after.guild.name,
event_type=Events.EDIT_MESSAGE,
is_user_bot=after.author.bot,
is_channel_nsfw=after.channel.nsfw,
message=f"AVANT: \n\n{before.clean_content}\n\nAPRÈS:\n\n{after.clean_content}",
discord_created_at=after.edited_at
1,
after.id,
after.author.id,
after.channel.id,
after.guild.id,
f"Message de {after.author.name}#{after.author.discriminator} dans {after.channel.name} édité:\n\n{before.clean_content}\n\n↓↓↓\n\n{after.clean_content}",
embed_message=f"Message de {after.author.mention} dans {after.channel.mention} édité:\n\n{before.clean_content}\n\n↓↓↓\n\n{after.clean_content}"
)

@commands.Cog.listener()
async def on_message_delete(self, deleted: discord.Message):
if not await self._wants_logging(deleted.guild.id):
if deleted.author.bot:
return

await self._log_action(
discord_message_id=deleted.id,
discord_user_id=deleted.author.id,
discord_channel_id=deleted.channel.id,
discord_server_id=deleted.guild.id,
discord_user_name=str(deleted.author),
discord_channel_name=deleted.channel.name,
discord_server_name=deleted.guild.name,
event_type=Events.DELETE_MESSAGE,
is_user_bot=deleted.author.bot,
is_channel_nsfw=deleted.channel.nsfw,
message=f"Message ID {deleted.id} supprimé\n\n{deleted.clean_content}",
2,
deleted.id,
deleted.author.id,
deleted.channel.id,
deleted.guild.id,
f"Message de {deleted.author.name}#{deleted.author.discriminator} dans {deleted.channel.name} supprimé:\n\n{deleted.clean_content}",
embed_message=f"Message de {deleted.author.mention} supprimé dans {deleted.channel.mention}:\n\n{deleted.clean_content}"
)

@commands.Cog.listener()
async def on_member_join(self, joined: discord.Member):
if not await self._wants_logging(joined.guild.id):
return

await self._log_action(
discord_user_id=joined.id,
discord_server_id=joined.guild.id,
discord_user_name=str(joined),
discord_server_name=joined.guild.name,
event_type=Events.JOINED_MEMBER,
is_user_bot=joined.bot
3,
None,
joined.id,
None,
joined.guild.id,
f"{joined.name}#{joined.discriminator} a rejoint le serveur",
embed_message=f'{joined.mention} a rejoint le serveur'
)

@commands.Cog.listener()
async def on_member_remove(self, left: discord.Member):
if not await self._wants_logging(left.guild.id):
return

await self._log_action(
discord_user_id=left.id,
discord_server_id=left.guild.id,
discord_user_name=str(left),
discord_server_name=left.guild.name,
event_type=Events.REMOVED_MEMBER,
is_user_bot=left.bot
4,
None,
left.id,
None,
left.guild.id,
f"{left.name}#{left.discriminator} a rejoint le serveur",
embed_message=f'{left.mention} a rejoint le serveur'
)

@commands.Cog.listener()
async def on_member_ban(self, guild: discord.Guild, banned: discord.Member):
if not await self._wants_logging(guild.id):
return

await self._log_action(
discord_user_id=banned.id,
discord_server_id=guild.id,
discord_user_name=str(banned),
discord_server_name=guild.name,
event_type=Events.REMOVED_MEMBER,
is_user_bot=banned.bot
5,
None,
banned.id,
None,
guild.id,
f"{banned.name} a été banni du serveur",
embed_message=f"{banned.mention} a été banni du serveur"
)

async def _wants_logging(self, server_id):
return await ConfigManager.get('log_events', server_id)

async def _log_channel_id(self, server_id):
return await ConfigManager.get('logging_channel_id', server_id)

async def _log_action(
self,
discord_message_id=None,
discord_user_id=None,
discord_channel_id=None,
discord_server_id=None,
discord_user_name=None,
discord_channel_name=None,
discord_server_name=None,
event_type=None,
is_user_bot=False,
is_channel_nsfw=False,
message=None,
discord_created_at=None
):
db = await get_database()
await db.execute(
"""
INSERT INTO logging(
discord_message_id,
discord_user_id,
discord_channel_id,
discord_server_id,
discord_user_name,
discord_channel_name,
discord_server_name,
event_type,
is_user_bot,
is_channel_nsfw,
message,
discord_created_at
)
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
""",

event_type,
discord_message_id,
discord_user_id,
discord_channel_id,
discord_server_id,
discord_user_name,
discord_channel_name,
discord_server_name,
event_type,
is_user_bot,
is_channel_nsfw,
message,
discord_created_at
embed_message = None
):
self.logging.info(f'Event {event_type} with message {message} on {discord_server_id} from {discord_user_id}')
if not await self._wants_logging(discord_server_id):
self.logging.info(f'Server {discord_server_id} doesnt want logging')
return

self.logging.info(f'Savin into database')
db = await get_database()
await db.execute(
'INSERT INTO logging('
'event_type, discord_message_id, '
'discord_user_id, discord_channel_id, discord_server_id, msg'
') VALUES($1, $2, $3, $4, $5, $6)',

str(event_type), discord_message_id, discord_user_id, discord_channel_id, discord_server_id, message
)

send_channel = await self._log_channel_id(discord_server_id)
self.logging.info(f'Cehck if server has send event channel')

if not send_channel:
return

real_send_channel = self.bot.get_channel(send_channel)

if real_send_channel is None:
return

self.logging.info(f'Prepare and send embed')
color, title = {
1: (self.colors['edited'], "Message édité"),
2: (self.colors['deleted'], "Message supprimé"),
3: (self.colors['created'], "Membre a rejoint le serveur"),
4: (self.colors['deleted'], "Membre a quitté le serveur"),
5: (self.colors['deleted'], "Membre a été bannu du serveur"),
}[event_type]

embed = discord.Embed()

embed.title = title
embed.colour = color
embed.description = message if embed_message is None else embed_message

await real_send_channel.send(embed=embed)


def setup(bot: commands.Bot):
bot.add_cog(Logging(bot))

+ 21
- 0
db/migrations/20190720150250_remake_logging_table.sql View File

@@ -0,0 +1,21 @@
-- migrate:up
ALTER TABLE logging
DROP COLUMN discord_username,
DROP COLUMN discord_channel_name,
DROP COLUMN discord_server_name,
DROP COLUMN is_user_bot,
DROP COLUMN is_channel_nsfw;

CREATE INDEX ON logging (discord_user_id);
CREATE INDEX ON logging (discord_server_id);

-- migrate:down
ALTER TABLE logging
ADD COLUMN discord_username VARCHAR,
ADD COLUMN discord_channel_name VARCHAR,
ADD COLUMN discord_server_name VARCHAR,
ADD COLUMN is_user_bot BOOLEAN DEFAULT FALSE,
ADD COLUMN is_channel_nsfw BOOLEAN DEFAULT FALSE;

DROP INDEX logging_discord_server_id_idx;
DROP INDEX logging_discord_user_id_idx;

+ 7
- 0
db/migrations/20190720151608_add_logging_channel_id_to_config.sql View File

@@ -0,0 +1,7 @@
-- migrate:up
ALTER TABLE config
ADD COLUMN logging_channel_id BIGINT;

-- migrate:down
ALTER TABLE config
DROP COLUMN logging_channel_id;

+ 19
- 7
db/schema.sql View File

@@ -85,7 +85,8 @@ CREATE TABLE public.config (
auto_role bigint,
log_events boolean DEFAULT false NOT NULL,
log_events_type integer DEFAULT '-1'::integer NOT NULL,
members_count_channel_id bigint
members_count_channel_id bigint,
logging_channel_id bigint
);


@@ -124,11 +125,6 @@ CREATE TABLE public.logging (
discord_user_id bigint,
discord_channel_id bigint,
discord_server_id bigint,
discord_username character varying,
discord_channel_name character varying,
discord_server_name character varying,
is_user_bot boolean DEFAULT false,
is_channel_nsfw boolean DEFAULT false,
msg text,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);
@@ -389,6 +385,20 @@ ALTER TABLE ONLY public.server_stats
CREATE INDEX logging_discord_message_id_idx ON public.logging USING btree (discord_message_id);


--
-- Name: logging_discord_server_id_idx; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX logging_discord_server_id_idx ON public.logging USING btree (discord_server_id);


--
-- Name: logging_discord_user_id_idx; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX logging_discord_user_id_idx ON public.logging USING btree (discord_user_id);


--
-- Name: scheduled_messages_enabled__idx; Type: INDEX; Schema: public; Owner: -
--
@@ -431,4 +441,6 @@ INSERT INTO public.schema_migrations (version) VALUES
('20190514185729'),
('20190530163611'),
('20190531190905'),
('20190620195223');
('20190620195223'),
('20190720150250'),
('20190720151608');

Loading…
Cancel
Save