insert2-cogs/gameutils.py

183 lines
7.2 KiB
Python
Raw Permalink Normal View History

2024-08-10 17:30:06 +00:00
import nextcord
from nextcord.ext import commands, application_checks
from nextcord import TextInputStyle
import random
import aiosqlite as sqlite3
import aiohttp
2024-08-11 19:14:26 +00:00
import traceback
import sys
2024-08-10 17:30:06 +00:00
from urllib.parse import urlparse
import os
2024-08-11 04:08:37 +00:00
class TurnModal(nextcord.ui.Modal):
2024-08-11 04:33:12 +00:00
def __init__(self,message,turnping,fristrun):
2024-08-11 04:08:37 +00:00
self.message = message
self.turnping = turnping
2024-08-11 04:33:12 +00:00
self.fristrun = fristrun
2024-08-11 04:08:37 +00:00
super().__init__(
2024-08-11 04:43:23 +00:00
f"{self.fristrun.name}/{self.fristrun.display_name}" if self.fristrun else f"set turn message/context"
2024-08-11 04:08:37 +00:00
)
self.text = nextcord.ui.TextInput(
label="text",
placeholder="text",
style=TextInputStyle.paragraph,
max_length=1700,
2024-08-12 11:27:27 +00:00
required=False,
2024-08-11 04:08:37 +00:00
)
self.add_item(self.text)
2024-08-11 04:12:44 +00:00
async def callback(self, interaction: nextcord.Interaction) -> None:
2024-08-11 04:39:32 +00:00
if not self.fristrun:
2024-08-12 19:35:28 +00:00
if not interaction.permissions.manage_roles:
await interaction.response.send_message("You don't have permission!", ephemeral=True)
return
2024-08-11 04:33:12 +00:00
await interaction.response.edit_message(content=self.message)
2024-08-11 04:46:28 +00:00
await interaction.followup.send(f"<{self.turnping}> it is now your turn!\n{self.text.value}")
2024-08-11 04:33:12 +00:00
else:
2024-08-11 04:38:44 +00:00
sentmsg = await interaction.response.send_message(self.message, view=GameView(), allowed_mentions=nextcord.AllowedMentions.none())
try:
sentmsg = await sentmsg.fetch()
await sentmsg.pin()
except:
pass
2024-08-12 19:36:37 +00:00
await interaction.followup.send(f"<@{self.fristrun.id}> it is now your turn!\n{self.text.value}")
2024-08-11 04:08:37 +00:00
2024-08-11 19:14:26 +00:00
async def on_error(self, error: nextcord.DiscordException, interaction: nextcord.Interaction):
error = traceback.format_exc()
print(error, file=sys.stderr)
message = f"```py\n{error[-1800:]}\n```\n Contact the bot owner if the error persists"
try:
await interaction.send(message, ephemeral=True)
except:
try:
await interaction.followup.send(message, ephemeral=True)
except:
await interaction.response.send_message(message, ephemeral=True)
2024-08-10 18:12:04 +00:00
class GameView(nextcord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@nextcord.ui.button(
label="Next Turn", style=nextcord.ButtonStyle.green, custom_id="gameutils:nextturn"
)
2024-08-10 20:48:23 +00:00
async def nextturn(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
2024-08-12 19:35:28 +00:00
if not interaction.permissions.manage_roles:
await interaction.response.send_message("You don't have permission!", ephemeral=True)
return
2024-08-10 18:41:44 +00:00
ogmsg = interaction.message.content.split("\n")
2024-08-10 21:01:48 +00:00
turnmessage = ogmsg[0]
2024-08-10 18:12:04 +00:00
ogmsg.pop(0)
2024-08-10 21:01:48 +00:00
message = f"{turnmessage}\n"
2024-08-10 18:12:04 +00:00
found = False
2024-08-10 19:48:39 +00:00
turnover = False
2024-08-10 19:02:33 +00:00
for i in range(len(ogmsg)):
if ogmsg[i].startswith("~"):
2024-08-10 19:31:52 +00:00
message = f"{message}{ogmsg[i]}\n"
2024-08-10 18:12:04 +00:00
continue
if not found:
2024-08-10 19:02:33 +00:00
newmsg = "~~" + ogmsg[i] + "~~"
message = message + newmsg + "\n"
2024-08-10 19:37:41 +00:00
found = True
try:
turnping = ogmsg[i+1].split("<")[1].split(">")[0]
except IndexError:
2024-08-11 02:45:06 +00:00
message = message + "The round is over"
2024-08-10 19:37:41 +00:00
turnover = True
2024-08-10 18:12:04 +00:00
continue
else:
2024-08-10 19:02:33 +00:00
message = message + ogmsg[i] + "\n"
2024-08-10 19:37:41 +00:00
if turnover:
2024-08-10 19:58:42 +00:00
await interaction.response.edit_message(content=message,view=None)
2024-08-11 02:45:06 +00:00
try:
await interaction.message.unpin()
except:
pass
await interaction.followup.send(f"The round has concluded")
2024-08-10 19:37:41 +00:00
else:
2024-08-11 04:33:12 +00:00
await interaction.response.send_modal(TurnModal(message,turnping,False))
2024-08-10 18:12:04 +00:00
return
2024-08-11 19:14:26 +00:00
async def on_error(self, error: nextcord.DiscordException, item: nextcord.ui.Item, interaction: nextcord.Interaction):
error = traceback.format_exc()
print(error, file=sys.stderr)
message = f"```py\n{error[-1800:]}\n```\n Contact the bot owner if the error persists"
try:
await interaction.send(message, ephemeral=True)
except:
try:
await interaction.followup.send(message, ephemeral=True)
except:
await interaction.response.send_message(message, ephemeral=True)
2024-08-10 17:30:06 +00:00
class gameutils(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
2024-08-10 19:55:42 +00:00
#errors if first loaded but is needed after
try:
self.bot.add_view(GameView())
except:
pass
2024-08-10 17:30:06 +00:00
2024-08-10 19:20:40 +00:00
@commands.Cog.listener('on_ready')
async def gameready(self):
self.bot.add_view(GameView())
2024-08-10 17:30:06 +00:00
@nextcord.slash_command(
name="turngen",
description="Roll the next turn for a certian role",
2024-08-17 14:41:35 +00:00
guild_ids=[1247883904178978927,699285282276507708,1059858639088779275],
2024-08-10 20:27:03 +00:00
default_member_permissions=nextcord.Permissions(manage_roles=True),
2024-08-10 17:30:06 +00:00
)
@nextcord.ext.application_checks.has_permissions(manage_roles=True)
2024-08-11 02:45:06 +00:00
async def turngen(self, interaction: nextcord.Interaction, role: nextcord.Role, turnmessage: str, disadvantagerole: nextcord.Role = nextcord.SlashOption(required=False)):
2024-08-10 21:01:48 +00:00
message = f"{turnmessage}\n"
2024-08-11 02:48:11 +00:00
users = role.members
2024-08-11 02:45:06 +00:00
if disadvantagerole:
disusers = disadvantagerole.members
2024-08-11 02:48:11 +00:00
users = list(set(users) - set(disusers))
2024-08-10 17:30:06 +00:00
random.shuffle(users)
2024-08-10 17:34:27 +00:00
for i in range(len(users)):
2024-08-10 18:43:57 +00:00
message = message + f"{str(i+1)}\. <@{users[i].id}>\n"
2024-08-11 02:45:06 +00:00
if disadvantagerole:
random.shuffle(disusers)
for i in range(len(disusers)):
2024-08-11 02:47:02 +00:00
message = message + f"{str(i+1+len(users))}\. <@{disusers[i].id}> **disadventage**\n"
2024-08-11 04:41:11 +00:00
await interaction.response.send_modal(TurnModal(message,None,users[0]))
2024-08-11 04:33:12 +00:00
#await interaction.followup.send(f"<@{users[0].id}> it is now your turn!")
2024-08-10 17:30:06 +00:00
2024-08-11 03:08:54 +00:00
@nextcord.slash_command(
name="dice",
description="Roll a number of dice",
2024-08-12 17:25:48 +00:00
#guild_ids=[1247883904178978927,699285282276507708],
integration_types=[
nextcord.IntegrationType.guild_install,
nextcord.IntegrationType.user_install,
],
contexts=[
nextcord.InteractionContextType.guild,
nextcord.InteractionContextType.bot_dm,
nextcord.InteractionContextType.private_channel,
],
force_global=True,
2024-08-11 03:08:54 +00:00
)
2024-08-11 03:17:01 +00:00
async def diceroll(self, interaction: nextcord.Interaction, number: int, sides: int, hidden: str = nextcord.SlashOption(choices=["Yes", "No"])):
2024-08-11 03:08:54 +00:00
message = f"May the odds ever be in your favor...\n"
2024-08-12 18:59:05 +00:00
fullnum = 0
2024-08-11 03:08:54 +00:00
for d in range(number):
2024-08-12 18:59:05 +00:00
num = random.randint(1, sides)
fullnum = fullnum + num
message = message + f":game_die: {str(d+1)}. {str(num)}\n"
message = message + f"sum: {fullnum}"
2024-08-11 03:13:17 +00:00
if hidden == "Yes":
await interaction.response.send_message(message, ephemeral=True)
else:
await interaction.response.send_message(message, ephemeral=False)
2024-08-11 03:08:54 +00:00
2024-08-10 17:30:06 +00:00
def setup(bot: commands.Bot):
bot.add_cog(gameutils(bot))