Skip to content

Commit

Permalink
ClashKing v4
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicTheDev committed Feb 13, 2024
1 parent eec875b commit c00fd4e
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 90 deletions.
2 changes: 1 addition & 1 deletion archived/competition.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async def signup_list(self, ctx: disnake.ApplicationCommandInteraction):
discord_id = r.get("discord_id")
member = self.bot.get_user(discord_id)
text += f"{player.name} | {player.tag} | {member.name}#{member.discriminator}\n"
except:
except Exception:
pass

if text == "":
Expand Down
24 changes: 10 additions & 14 deletions classes/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from utility.login import coc_login


class CustomClient(commands.AutoShardedBot):
def __init__(self, config: Config, command_prefix: str, help_command, intents: disnake.Intents, scheduler: AsyncIOScheduler, shard_count: int | None, chunk_guilds_at_startup: bool):
super().__init__(command_prefix=command_prefix, help_command=help_command, intents=intents, shard_count=shard_count, chunk_guilds_at_startup=chunk_guilds_at_startup)
Expand Down Expand Up @@ -142,6 +143,7 @@ def __init__(self, config: Config, command_prefix: str, help_command, intents: d
self.OUR_GUILDS = set()
self.badge_guild = []
self.EXTENSION_LIST = []
self.STARTED_CHUNK = set()


def clean_string(self, text: str):
Expand Down Expand Up @@ -671,33 +673,26 @@ async def get_player_history(self, player_tag: str):
async def white_list_check(self, ctx, command_name):
if ctx.author.id == 706149153431879760:
return True
member = ctx.author
roles = (await ctx.guild.getch_member(member_id=ctx.author.id)).roles
if disnake.utils.get(roles, name="ClashKing Perms") != None:

member = await ctx.guild.getch_member(member_id=ctx.author.id)
if disnake.utils.get(member.roles, name="ClashKing Perms") is not None:
return True

commandd = command_name
guild = ctx.guild.id
results = self.whitelist.find({"$and" : [
{"command": commandd},
results = self.whitelist.find({"$and" : [
{"command": command_name},
{"server" : guild}
]})

if results is None:
return False

limit = await self.whitelist.count_documents(filter={"$and" : [
{"command": commandd},
{"server" : guild}
]})

perms = False
for role in await results.to_list(length=limit):
for role in await results.to_list(length=None):
role_ = role.get("role_user")
is_role = role.get("is_role")
if is_role:
role_ = ctx.guild.get_role(role_)
if member in role_.members:
if disnake.utils.get(member.roles, id=int(role_)) is not None:
return True
else:
if member.id == role_:
Expand All @@ -706,6 +701,7 @@ async def white_list_check(self, ctx, command_name):
return perms



def command_names(self):
commands = []
for command_ in self.slash_commands:
Expand Down
18 changes: 11 additions & 7 deletions commands/eval/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ async def refresh(self, ctx: disnake.ApplicationCommandInteraction,
test: (optional) test mode, won't make any changes
advanced_mode: (optional) choose what role types to evaluate
"""

await ctx.response.defer()
if role_or_user is None:
role_or_user = ctx.user
Expand All @@ -46,6 +45,17 @@ async def refresh(self, ctx: disnake.ApplicationCommandInteraction,
raise MessageException("Missing Manage Server Permissions and/or not whitelisted for this command (`/whitelist add`")

db_server = await self.bot.ck_client.get_server_settings(server_id=ctx.guild_id)
if not ctx.guild.chunked:
embed = disnake.Embed(
description=f"The bot is pulling your member info from the discord API, please try again in a few minutes.",
color=db_server.embed_color)
await ctx.edit_original_message(embed=embed)
if ctx.guild.id not in self.bot.STARTED_CHUNK:
await ctx.guild.chunk(cache=True)
else:
self.bot.STARTED_CHUNK.add(ctx.guild.id)
return

if advanced_mode:
options = []
for option in DEFAULT_EVAL_ROLE_TYPES:
Expand Down Expand Up @@ -73,12 +83,6 @@ async def refresh(self, ctx: disnake.ApplicationCommandInteraction,
eval_types = DEFAULT_EVAL_ROLE_TYPES

if isinstance(role_or_user, disnake.Role):
if not ctx.guild.chunked:
embed = disnake.Embed(
description="The bot is pulling your member info from the discord API, please try again in a few minutes.",
color=db_server.embed_color)
await ctx.edit_original_message(embed=embed)
return
members = role_or_user.members
clans = await self.bot.clan_db.distinct("tag", filter={"generalRole": role_or_user.id})
if not clans:
Expand Down
10 changes: 10 additions & 0 deletions commands/rosters/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,16 @@ async def roster_role(self, ctx: disnake.ApplicationCommandInteraction, roster:
@commands.check_any(commands.has_permissions(manage_guild=True), check_commands())
async def roster_role_refresh(self, ctx: disnake.ApplicationCommandInteraction, roster: str):
await ctx.response.defer()
if not ctx.guild.chunked:
embed = disnake.Embed(
description=f"The bot is pulling your member info from the discord API, please try again in a few minutes.",
color=disnake.Color.green())
await ctx.edit_original_message(embed=embed)
if ctx.guild.id not in self.bot.STARTED_CHUNK:
await ctx.guild.chunk(cache=True)
else:
self.bot.STARTED_CHUNK.add(ctx.guild.id)
return
if roster != "REFRESH ALL":
_roster = Roster(bot=self.bot)
await _roster.find_roster(guild=ctx.guild, alias=roster)
Expand Down
2 changes: 1 addition & 1 deletion discord/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async def on_guild_join(self, guild:disnake.Guild):
"One last thing to note - the bot is still under heavy development & is relatively young, so please reach out with any issues & if you end up enjoying what is here, consider using Creator Code ClashKing " \
"in-game to help support the project"
results = await self.bot.server_db.find_one({"server": guild.id})
botAdmin = guild.get_member(self.bot.user.id).guild_permissions.administrator
botAdmin = (await guild.getch_member(self.bot.user.id)).guild_permissions.administrator
if results is None:
await self.bot.server_db.insert_one({
"server": guild.id,
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
message_content=True
)

bot = CustomClient(command_prefix="??", help_command=None, intents=intents, scheduler=scheduler, config=config, shard_count=None, chunk_guilds_at_startup=True)
bot = CustomClient(command_prefix="??", help_command=None, intents=intents, scheduler=scheduler, config=config, shard_count=None, chunk_guilds_at_startup=False)


initial_extensions = [
Expand Down
74 changes: 8 additions & 66 deletions utility/discord_utils.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import os

import coc
import disnake
from disnake.ext import commands
import classes.bot
from assets.emojiDictionary import emojiDictionary, legend_emojis
from typing import Callable, Union
from exceptions.CustomExceptions import MissingWebhookPerms
from urllib.request import Request, urlopen
import io
from exceptions.CustomExceptions import *
from datetime import datetime
from operator import attrgetter
import aiohttp
from dotenv import load_dotenv
from typing import List
import motor.motor_asyncio


def check_commands():
async def predicate(ctx: disnake.ApplicationCommandInteraction):
if ctx.author.id == 706149153431879760:
return True
roles = (await ctx.guild.getch_member(member_id=ctx.author.id)).roles
if disnake.utils.get(roles, name="ClashKing Perms") != None:

member = await ctx.guild.getch_member(member_id=ctx.author.id)
if disnake.utils.get(member.roles, name="ClashKing Perms") != None:
return True
db_client = motor.motor_asyncio.AsyncIOMotorClient(os.getenv("DB_LOGIN"))
whitelist = db_client.usafam.whitelist
Expand All @@ -41,18 +34,12 @@ async def predicate(ctx: disnake.ApplicationCommandInteraction):
if results is None:
return False

limit = await whitelist.count_documents(filter={"$and" : [
{"command": commandd},
{"server" : guild}
]})

perms = False
for role in await results.to_list(length=limit):
for role in await results.to_list(length=None):
role_ = role.get("role_user")
is_role = role.get("is_role")
if is_role:
role_ = ctx.guild.get_role(role_)
if member in role_.members:
if disnake.utils.get(member.roles, id=int(role_)) is not None:
return True
else:
if member.id == role_:
Expand Down Expand Up @@ -119,7 +106,7 @@ def check(res: disnake.MessageInteraction):
return valid_value


async def basic_embed_modal(bot, ctx: disnake.ApplicationCommandInteraction, previous_embed=None):
'''async def basic_embed_modal(bot, ctx: disnake.ApplicationCommandInteraction, previous_embed=None):
components = [
disnake.ui.TextInput(
label=f"Embed Title",
Expand Down Expand Up @@ -194,7 +181,7 @@ def check(res: disnake.ModalInteraction):
embed = await generate_embed(bot=bot, our_embed=our_embed, embed=previous_embed)
await modal_inter.response.defer()
return (modal_inter, embed)
return (modal_inter, embed)'''


def iter_embed_creation(base_embed: disnake.Embed, iter: List, scheme: str, brk: int = 50) -> List[disnake.Embed]:
Expand Down Expand Up @@ -225,51 +212,6 @@ def decorator(func):
return decorator



async def generate_embed(bot, our_embed: dict, embed=None):
if embed is None:
embed = disnake.Embed()
for attribute, embed_field in our_embed.items():
if embed_field is None or embed_field == "":
continue
attribute: str
if "field" in attribute:
if embed_field["name"] is None or embed_field == "":
continue
embed.insert_field_at(index=int(attribute.split("_")[1]) - 1, name=embed_field["name"],
value=embed_field["value"], inline=embed_field["inline"])
elif "image" in attribute:
if embed_field != "" and embed_field != "None":
embed_field = await permanent_image(bot, embed_field)
if embed_field == "None":
embed._image = None
else:
embed.set_image(url=embed_field)
elif "thumbnail" in attribute:
if embed_field != "" and embed_field != "None":
embed_field = await permanent_image(bot, embed_field)
if embed_field == "None":
embed._thumbnail = None
else:
embed.set_thumbnail(url=embed_field)
elif "footer" in attribute:
if embed_field["text"] is None:
continue
embed.set_footer(icon_url=embed_field["icon"], text=embed_field["text"])
elif "author" in attribute:
if embed_field["text"] is None:
continue
embed.set_author(icon_url=embed_field["icon"], name=embed_field["text"])
else:
if len(attribute.split(".")) == 2:
obj = attrgetter(attribute.split(".")[0])(embed)
setattr(obj, attribute.split(".")[1], embed_field)
else:
setattr(embed, attribute, embed_field)

return embed


async def get_webhook_for_channel(bot, channel: Union[disnake.TextChannel, disnake.Thread]) -> disnake.Webhook:
try:
if isinstance(channel, disnake.Thread):
Expand Down

0 comments on commit c00fd4e

Please sign in to comment.