2022-12-08 22:21:53 +01:00
import discord
import re
import os
2023-03-31 16:11:03 +02:00
from config import debug , curs_data
2022-12-08 22:21:53 +01:00
2023-03-31 14:09:06 +02:00
class ManageChat ( discord . Cog ) :
2022-12-08 22:21:53 +01:00
def __init__ ( self , bot : discord . Bot ) - > None :
super ( ) . __init__ ( )
self . bot = bot
2023-03-31 14:09:06 +02:00
@discord.slash_command (
name = " cancel " , description = " Cancel the last message sent into a channel "
)
2022-12-08 22:21:53 +01:00
async def cancel ( self , ctx : discord . ApplicationContext ) :
2023-03-31 14:09:06 +02:00
debug (
f " The user { ctx . author } ran the cancel command in the channel { ctx . channel } of the guild { ctx . guild } , named { ctx . guild . name } "
)
# check if the guild is in the database
2023-03-31 20:46:08 +02:00
curs_data . execute (
" SELECT * FROM data WHERE guild_id = ? " , ( ctx . guild . id , ) )
2023-03-31 16:11:03 +02:00
if curs_data . fetchone ( ) is None :
2023-03-31 14:09:06 +02:00
await ctx . respond (
" This server is not setup, please run /setup " , ephemeral = True
)
2022-12-08 22:21:53 +01:00
return
2023-03-31 14:09:06 +02:00
# get the last message sent by the bot in the cha where the command was sent
2022-12-08 22:21:53 +01:00
last_message = await ctx . channel . fetch_message ( ctx . channel . last_message_id )
2023-03-31 14:09:06 +02:00
# delete the message
2022-12-08 22:21:53 +01:00
await last_message . delete ( )
await ctx . respond ( " The last message has been deleted " , ephemeral = True )
2023-03-31 14:09:06 +02:00
# add a slash command called "clear" that deletes all the messages in the channel
@discord.slash_command (
name = " clear " , description = " Clear all the messages in the channel "
)
2022-12-08 22:21:53 +01:00
async def clear ( self , ctx : discord . ApplicationContext ) :
2023-03-31 14:09:06 +02:00
debug (
f " The user { ctx . author . name } ran the clear command command in the channel { ctx . channel } of the guild { ctx . guild } , named { ctx . guild . name } "
)
2022-12-08 22:21:53 +01:00
await ctx . respond ( " messages deleted! " , ephemeral = True )
return await ctx . channel . purge ( )
2023-03-31 14:09:06 +02:00
@discord.slash_command (
name = " transcript " ,
description = " Get a transcript of the messages that have been sent in this channel intoa text file " ,
)
@discord.option (
name = " channel_send " ,
description = " The channel to send the transcript to " ,
required = False ,
)
async def transcript (
self , ctx : discord . ApplicationContext , channel_send : discord . TextChannel = None
) :
debug (
f " The user { ctx . author . name } ran the transcript command command in the channel { ctx . channel } of the guild { ctx . guild } , named { ctx . guild . name } "
)
# save all the messages in the channel in a txt file and send it
2022-12-08 22:21:53 +01:00
messages = await ctx . channel . history ( limit = None ) . flatten ( )
messages . reverse ( )
transcript = " "
2023-03-31 14:09:06 +02:00
# defer the response
await ctx . defer ( ) # defer the response so that the bot doesn't say that it's thinking
2022-12-08 22:21:53 +01:00
for msg in messages :
if msg . author . bot :
transcript + = f " Botator: { msg . content } \n "
else :
mentions = re . findall ( r " <@!? \ d+> " , msg . content )
2023-03-31 14:09:06 +02:00
# then replace each mention with the name of the user
2022-12-08 22:21:53 +01:00
for mention in mentions :
2023-03-31 14:09:06 +02:00
# get the user id
2022-12-08 22:21:53 +01:00
id = mention [ 2 : - 1 ]
2023-03-31 14:09:06 +02:00
# get the user
2022-12-08 22:21:53 +01:00
user = await self . bot . fetch_user ( id )
2023-03-31 14:09:06 +02:00
# replace the mention with the name
msg . content = msg . content . replace (
mention , msg . guild . get_member ( user . id ) . name
)
2022-12-19 20:15:19 +01:00
transcript + = f " { msg . author . name } : { msg . content } \n "
2023-03-31 14:09:06 +02:00
# save the transcript in a txt file called transcript.txt. If the file already exists, delete it and create a new one
# check if the file exists
2022-12-08 22:21:53 +01:00
if os . path . exists ( " transcript.txt " ) :
os . remove ( " transcript.txt " )
f = open ( " transcript.txt " , " w " )
f . write ( transcript )
f . close ( )
2023-03-31 14:09:06 +02:00
last_message : discord . Message = await ctx . channel . fetch_message (
ctx . channel . last_message_id
)
2023-03-31 20:46:08 +02:00
new_file_name = f " transcript_ { ctx . guild . name } _ { ctx . channel . name } _ { last_message . created_at . strftime ( ' %d - % B- % Y ' ) } .txt "
2023-03-31 14:09:06 +02:00
# rename the file with the name of the channel and the date in this format: transcript_servername_channelname_dd-month-yyyy.txt ex : transcript_Botator_Testing_12-may-2021.txt
os . rename (
" transcript.txt " ,
2023-03-31 20:46:08 +02:00
new_file_name ,
2023-03-31 14:09:06 +02:00
)
# send the file in a private message to the user who ran the command
2023-03-31 20:46:08 +02:00
# TODO: rework so as to give the choice of a private send or a public send
2022-12-08 22:21:53 +01:00
if channel_send is None :
2023-03-31 14:09:06 +02:00
await ctx . respond (
file = discord . File (
2023-03-31 20:46:08 +02:00
new_file_name
2023-03-31 14:09:06 +02:00
) ,
ephemeral = True ,
)
2022-12-08 22:21:53 +01:00
else :
2023-03-31 14:09:06 +02:00
await channel_send . send (
file = discord . File (
2023-03-31 20:46:08 +02:00
new_file_name
2023-03-31 14:09:06 +02:00
)
)
2022-12-12 12:55:13 +01:00
await ctx . respond ( " Transcript sent! " , ephemeral = True , delete_after = 5 )
2023-03-31 14:09:06 +02:00
await ctx . author . send (
file = discord . File (
2023-03-31 20:46:08 +02:00
new_file_name
2023-03-31 14:09:06 +02:00
)
)
# delete the file
os . remove (
2023-03-31 20:46:08 +02:00
new_file_name
2023-03-31 14:09:06 +02:00
)