rewrite speech bubble

This commit is contained in:
insert 2024-08-07 14:37:15 -04:00
parent 9652f7f8b6
commit 3420904ade
Signed by: insert
GPG key ID: A70775C389ACF105

View file

@ -1,66 +1,113 @@
import disnake import nextcord
from disnake.ext import commands from nextcord.ext import commands
from disnake import TextInputStyle from nextcord import TextInputStyle
from dotenv import load_dotenv
from random import randint from random import randint
import aiosqlite as sqlite3 import aiosqlite as sqlite3
import aiohttp import aiohttp
from urllib.parse import urlparse from urllib.parse import urlparse
import os import os
class ApplicationModal(disnake.ui.Modal):
def __init__(self, user): class ApplicationView(nextcord.ui.View):
def __init__(self,bot):
super().__init__(timeout=None)
self.bot = bot
@nextcord.ui.button(
label="Approve", style=nextcord.ButtonStyle.green, custom_id="speechbubble:approve"
)
async def approve(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
ogmsg = interaction.message.embeds
embed = ogmsg[0]
if interaction.user.id != int(embed.fields[0].value):
await interaction.response.send_message("you may not modify this speech bubble!", ephemeral=True)
return
embed.add_field(
name="Status",
value="Approved",
inline=False,
)
await self.bot.cur.execute(f"""
INSERT INTO userinfo VALUES
({interaction.guild_id}, {int(embed.fields[0].value)}, '{embed.fields[1].value}', {int(embed.fields[2].value)})
""")
await self.bot.db.commit()
await interaction.response.edit_message(embed=embed, view=None)
return
@nextcord.ui.button(
label="Deny", style=nextcord.ButtonStyle.red, custom_id="speechbubble:deny"
)
async def deny(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
ogmsg = interaction.message.embeds
embed = ogmsg[0]
if interaction.user.id != int(embed.fields[0].value):
await interaction.response.send_message("you may not modify this speech bubble!", ephemeral=True)
return
embed.add_field(
name="Status",
value="Denied",
inline=False,
)
await interaction.response.edit_message(embed=embed, view=None)
return
class ApplicationModal(nextcord.ui.Modal):
def __init__(self,user,bot):
self.victim = user self.victim = user
components = [ self.bot = bot
disnake.ui.TextInput( super().__init__(
f"create a speechbubble for {user.name}"
)
self.image = nextcord.ui.TextInput(
label="link to image", label="link to image",
placeholder="https://media.discordapp.net/goofyimg.png", placeholder="https://media.discordapp.net/goofyimg.png",
custom_id="image",
style=TextInputStyle.short, style=TextInputStyle.short,
max_length=400, max_length=400,
), )
disnake.ui.TextInput( self.add_item(self.image)
self.chance = nextcord.ui.TextInput(
label="chance: a 1 in x chance", label="chance: a 1 in x chance",
placeholder="10", placeholder="10",
custom_id="chance: a 1 in x chance",
style=TextInputStyle.short, style=TextInputStyle.short,
max_length=5, max_length=5,
), )
] self.add_item(self.chance)
super().__init__(title=f"create a speechbubble for {user.name}", components=components)
async def callback(self, inter: disnake.ModalInteraction): async def callback(self, interaction: nextcord.Interaction) -> None:
speechbubble = inter.text_values.items() embed = nextcord.Embed(title="New speech bubble!", color=nextcord.Colour.green())
embed = disnake.Embed(title="New speech bubble!", color=disnake.Colour.green())
embed.add_field( embed.add_field(
name="User ID", name="User ID",
value=self.victim.id, value=self.victim.id,
inline=False, inline=False,
) )
for key, value in speechbubble: print(self.image.value)
if key == "image": if urlparse(self.image.value).netloc != 'media.discordapp.net' and urlparse(self.image.value).netloc != 'cdn.discordapp.com':
if urlparse(value).netloc != 'media.discordapp.net' and urlparse(value).netloc != 'cdn.discordapp.com': await interaction.send_message(f"for security reasons, the bot only accepts images from media.discordapp.net or cdn.discordapp.com", ephemeral=True)
await inter.response.send_message(f"for security reasons, the bot only accepts images from media.discordapp.net or cdn.discordapp.com", ephemeral=True)
return return
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get(value) as response: async with session.get(self.image.value) as response:
if (response.status == 200) and (response.headers['content-type'] == "image/png" or response.headers['content-type'] == "image/jpeg" or response.headers['content-type'] == "image/jpg"): if (response.status == 200) and (response.headers['content-type'] == "image/png" or response.headers['content-type'] == "image/jpeg" or response.headers['content-type'] == "image/jpg"):
pass pass
else: else:
await inter.response.send_message(f"you did not supply an image {response.status} {response.headers['content-type']}", ephemeral=True) await interaction.send(f"you did not supply an image {response.status} {response.headers['content-type']}", ephemeral=True)
return return
else: if not self.chance.value.isdigit():
if not value.isdigit(): await interaction.send("you supplied words for a number", ephemeral=True)
await inter.response.send_message("you supplied words for a number", ephemeral=True)
return return
embed.add_field( embed.add_field(
name=key, name="Image",
value=value, value=self.image.value,
inline=False, inline=False,
) )
await inter.response.send_message(f"<@{self.victim.id}> {inter.user.name} proposes the following speech bubble for you:", embed=embed, components=[ embed.add_field(
disnake.ui.Button(label="Approve", style=disnake.ButtonStyle.success, custom_id="Approve"), name="Chance, a 1 in x chance",
disnake.ui.Button(label="Deny", style=disnake.ButtonStyle.danger, custom_id="Deny"),],) value=self.chance.value,
inline=False,
)
await interaction.send(f"<@{self.victim.id}> {interaction.user.name} proposes the following speech bubble for you:", embed=embed, view=ApplicationView(self.bot))
class speechbubble(commands.Cog): class speechbubble(commands.Cog):
@ -68,34 +115,9 @@ class speechbubble(commands.Cog):
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot):
self.bot = bot self.bot = bot
@commands.Cog.listener("on_button_click") @commands.Cog.listener('on_ready')
async def button_listener(self, inter: disnake.MessageInteraction): async def ready(self):
ogmsg = inter.message.embeds self.add_view(ApplicationView())
embed = ogmsg[0]
if inter.user.id != int(embed.fields[0].value):
await inter.response.send_message("you may not modify this speech bubble!", ephemeral=True)
return
if inter.component.custom_id == "Approve":
embed.add_field(
name="Status",
value="Approved",
inline=False,
)
await inter.response.edit_message(embed=embed, components=[])
await self.bot.cur.execute(f"""
INSERT INTO userinfo VALUES
({inter.guild_id}, {int(embed.fields[0].value)}, '{embed.fields[1].value}', {int(embed.fields[2].value)})
""")
await self.bot.db.commit()
return
if inter.component.custom_id == "Deny":
embed.add_field(
name="Status",
value="Denied",
inline=False,
)
await inter.response.edit_message(embed=embed, components=[])
return
@commands.Cog.listener('on_message') @commands.Cog.listener('on_message')
async def on_message(self,message): async def on_message(self,message):
@ -107,27 +129,28 @@ class speechbubble(commands.Cog):
if randint(1, chance) == 1: if randint(1, chance) == 1:
await message.channel.send(imagelink) await message.channel.send(imagelink)
@commands.slash_command( @nextcord.slash_command(
name="create", name="create",
description="create a speech bubble for a user", description="create a speech bubble for a user",
) )
async def create(self, inter: disnake.ApplicationCommandInteraction, victim: disnake.Member = commands.Param(name="victim")): async def create(self, interaction: nextcord.Interaction, victim: nextcord.Member = nextcord.SlashOption(name="victim")):
if inter.user.id == victim.id: if interaction.user.id == victim.id:
await inter.response.send_message("you may not assign yourself a speech bubble, try someone else!", ephemeral=True) await interaction.response.send_message("you may not assign yourself a speech bubble, try someone else!", ephemeral=True)
return return
await inter.response.send_modal(modal=ApplicationModal(victim)) await interaction.response.send_modal(ApplicationModal(victim,self.bot))
@commands.slash_command( @nextcord.slash_command(
name="removeme", name="removeme",
description="Removes you from the database", description="Removes you from the database",
guild_ids=[732793772697583623],
) )
async def removeme(self, inter: disnake.ApplicationCommandInteraction, universal: str = commands.Param(choices=["yes", "no"])): async def removeme(self, interaction: nextcord.Interaction, universal: str = nextcord.SlashOption(choices=["yes", "no"])):
if universal == "yes": if universal == "yes":
await self.bot.cur.execute(f"DELETE FROM userinfo WHERE userid = {inter.user.id}") await self.bot.cur.execute(f"DELETE FROM userinfo WHERE userid = {interaction.user.id}")
elif universal == "no": elif universal == "no":
await self.bot.cur.execute(f"DELETE FROM userinfo WHERE userid = {inter.user.id} AND serverid = {inter.guild_id}") await self.bot.cur.execute(f"DELETE FROM userinfo WHERE userid = {interaction.user.id} AND serverid = {interaction.guild_id}")
await self.bot.db.commit() await self.bot.db.commit()
await inter.response.send_message("Done!", ephemeral=True) await interaction.response.send_message("Done!", ephemeral=True)
def setup(bot: commands.Bot): def setup(bot: commands.Bot):
bot.add_cog(speechbubble(bot)) bot.add_cog(speechbubble(bot))