Files
Botator/code/cogs/chat.py

95 lines
4.5 KiB
Python
Raw Normal View History

2022-12-08 22:21:53 +01:00
import discord
2023-03-09 16:49:42 +01:00
from discord.ext import commands
2023-03-09 16:22:15 +01:00
from config import debug, c, max_uses, cp, conn, connp, webhook_url
import makeprompt as mp
2023-03-09 16:22:15 +01:00
import aiohttp
class MyModal(discord.ui.Modal):
def __init__(self, message):
super().__init__(title="Downvote")
self.add_item(discord.ui.InputText(label="Reason", style=discord.InputTextStyle.long))
self.message = message
async def callback(self, interaction: discord.Interaction):
debug("Downvote sent !")
embed = discord.Embed(title="Thanks for your feedback !", description="Your downvote has been sent to the developers. Thanks for your help !", color=discord.Color.og_blurple())
embed.add_field(name="Message", value=self.children[0].value)
await interaction.response.send_message(embed=embed, ephemeral=True)
2023-03-09 17:03:39 +01:00
if webhook_url != "" and webhook_url != None:
2023-03-09 16:22:15 +01:00
session = aiohttp.ClientSession()
webhook = discord.Webhook.from_url(webhook_url, session=session)
embed = discord.Embed(title="Downvote", description=f"Downvote recieved!", color=discord.Color.og_blurple())
2023-03-09 16:49:42 +01:00
embed.add_field(name="Reason", value=self.children[0].value, inline=True)
embed.add_field(name="Author", value=self.message.author.mention, inline=True)
embed.add_field(name="Channel", value=self.message.channel.name, inline=True)
embed.add_field(name="Guild", value=self.message.guild.name, inline=True)
2023-03-09 17:25:45 +01:00
history = await self.message.channel.history(limit=5, before=self.message).flatten()
history.reverse()
users = []
fake_users = []
for user in history:
if user.author not in users:
#we anonimize the user, so user1, user2, user3, etc
fake_users.append(f"user{len(fake_users)+1}")
users.append(user.author)
if self.message.author not in users:
fake_users.append(f"user{len(fake_users)+1}")
users.append(self.message.author)
2023-03-09 16:49:42 +01:00
for msg in history:
2023-03-09 17:25:45 +01:00
uname = fake_users[users.index(msg.author)]
2023-03-09 17:01:06 +01:00
if len(msg.content) > 1023:
2023-03-09 17:25:45 +01:00
embed.add_field(name=f"{uname} said", value=msg.content[:1023], inline=False)
2023-03-09 17:01:06 +01:00
else:
2023-03-09 17:25:45 +01:00
embed.add_field(name=f"{uname} said", value=msg.content, inline=False)
if len(self.message.content) > 1021:
uname = fake_users[users.index(self.message.author)]
embed.add_field(name=f"{uname} said", value="*"+self.message.content[:1021]+"*", inline=False)
else:
uname = fake_users[users.index(self.message.author)]
embed.add_field(name=f"{uname} said", value="*"+self.message.content+"*", inline=False)
2023-03-09 16:49:42 +01:00
await webhook.send(embed=embed)
2023-03-09 17:03:39 +01:00
else:
2023-03-09 16:49:42 +01:00
debug("Error while sending webhook, probably no webhook is set up in the .env file")
2023-03-09 16:22:15 +01:00
2022-12-08 22:21:53 +01:00
class Chat (discord.Cog) :
def __init__(self, bot: discord.Bot):
super().__init__()
self.bot = bot
@discord.Cog.listener()
async def on_message(self, message: discord.Message):
await mp.chat_process(self, message)
2022-12-12 12:58:45 +01:00
2022-12-08 22:21:53 +01:00
@discord.slash_command(name="say", description="Say a message")
async def say(self, ctx: discord.ApplicationContext, message: str):
await ctx.respond("Message sent !", ephemeral=True)
2022-12-10 09:36:06 +01:00
await ctx.send(message)
2022-12-16 12:51:46 +01:00
@discord.slash_command(name="redo", description="Redo a message")
async def redo(self, ctx: discord.ApplicationContext):
2022-12-16 15:43:32 +01:00
history = await ctx.channel.history(limit=2).flatten()
message_to_delete = history[0]
message_to_redo = history[1]
if message_to_delete.author.id == self.bot.user.id:
await message_to_delete.delete()
2022-12-16 12:51:46 +01:00
else:
message_to_redo=history[0]
2022-12-19 19:55:25 +01:00
await ctx.respond("Message redone !", ephemeral=True)
2023-03-09 16:22:15 +01:00
await mp.chat_process(self, message_to_redo)
@discord.message_command(name="Downvote", description="Downvote a message")
2023-03-09 16:49:42 +01:00
@commands.cooldown(1, 60, commands.BucketType.user)
2023-03-09 16:22:15 +01:00
async def downvote(self, ctx: discord.ApplicationContext, message: discord.Message):
modal = MyModal(message)
2023-03-09 16:49:42 +01:00
await ctx.send_modal(modal)
@downvote.error
async def downvote_error(self, ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.respond("You are on cooldown !", ephemeral=True)
else:
debug(error)
raise error