Browse Source

много всего

huy
gsd 2 years ago
parent
commit
2f0e8a2eec
  1. 11
      admin_ext/ban.py
  2. 21
      bot.py
  3. 110
      other_ext/register.py
  4. 51
      other_ext/url_checker.py
  5. 58
      other_ext/webhook_helper.py
  6. 43
      player.py
  7. 16
      user_ext/profile.py

11
admin_ext/ban.py

@ -1,7 +1,9 @@
import discord
class Extension:
core = None
def __init__(self, core):
self.core = core
@core.tree.command(name = "ban", description = "Забанить игрока на серверах")
@discord.app_commands.describe(profile=core.ANY_INPUT, reason="причина", minutes="время бана | 0 - навсегда")
async def ban_player(
@ -11,6 +13,9 @@ class Extension:
minutes: int = 0
):
await interaction.response.defer(thinking=True)
steam64 = await core.GetSteam64OfDiscord(interaction.user)
player = await core.GetPlayer(profile, steam64, False)
return await interaction.followup.send(f'{await player.ban(reason, minutes)}', ephemeral=False)
return await interaction.followup.send(f'{await self.__call__(profile, reason, minutes, interaction.user)}', ephemeral=False)
async def __call__(self, profile, reason, minutes, requester):
steam64 = await self.core.GetSteam64OfDiscord(requester)
player = await self.core.GetPlayer(profile, steam64, False)
return await player.ban(reason, minutes)

21
bot.py

@ -12,19 +12,21 @@ import asyncio, sys
#Скрыть сообщение если надо ephemeral=True
class DiscordClient(discord.Client):
main_server_id = 0
ANY_INPUT = "ссылка на стим | имя игрока | стимид | ид бана через #"
discord2steam_cache = {}
backend_url = ""
secret_key = ""
stats = {}
show_stats_prev = 0
loaded_extensions = {}
def __init__(self, backend_url, secret_key):
self.backend_url = backend_url
self.secret_key = secret_key
self.StartUpPreloadStats()
###################################################
super().__init__(intents=discord.Intents.default())
super().__init__(intents=discord.Intents.all())
self.tree = app_commands.CommandTree(self)
self.load_extensions(['user_ext', 'admin_ext', 'other_ext'])
###################################################
@ -41,14 +43,15 @@ class DiscordClient(discord.Client):
if ext != ".py":
continue
print(f"Loading: {extension}")
__import__(extension).Extension(self)
self.loaded_extensions[extension] = __import__(extension).Extension(self)
sys.path.pop(0)
async def setup_hook(self):
await self.GetStats()
print("sync tree")
if os.getenv("MAIN_DISCORD_SERVER_ID",""):
await self.tree.sync(guild=discord.Object(int(os.getenv("MAIN_DISCORD_SERVER_ID"))))
self.main_server_id = int(os.getenv("MAIN_DISCORD_SERVER_ID"))
await self.tree.sync(guild=discord.Object(self.main_server_id))
await self.tree.sync()
def setup_events(self):
@ -60,6 +63,8 @@ class DiscordClient(discord.Client):
async def on_app_command_error(interaction, error):
if isinstance(error.original, CannotCastToSteamID):
return await interaction.followup.send("Не возможно найти такой профиль, попробуй написать иные данные!", ephemeral=True)
elif isinstance(error.original, discord.app_commands.errors.NoPrivateMessage):
return await interaction.followup.send("Вызванная тобой команда не работает в личных сообщениях", ephemeral=False)
elif isinstance(error.original, LowPermition):
return await interaction.followup.send("Это не для тебя и не для таких как ты сделано...", ephemeral=True)
elif isinstance(error.original, AdminLowPermition):
@ -70,6 +75,8 @@ class DiscordClient(discord.Client):
return await interaction.followup.send("Ошибка на стороне сервера в исполнении говнокода, стоит подождать или позвать помощь", ephemeral=False)
elif isinstance(error.original, discord.errors.NotFound):
return await interaction.followup.send("Слишком долгий ответ со стороны сервера, причины:\n1) Возможно бекенд сдох\n2)Cлишком долгий незапланированный ответ с сервера\n3)Стоит позвать помощь", ephemeral=True)
elif isinstance(error.original, NeedDiscordAuthOfSteam):
return await interaction.followup.send("У тебя не привязан профиль стима к дискорду, ничего подобного делать тебе нельзя", ephemeral=True)
traceback.print_exc()
return await interaction.followup.send("Возникла необратимая ошибка, хз что случилось. Попробуй еще раз!", ephemeral=True)
@ -79,8 +86,12 @@ class DiscordClient(discord.Client):
async with aiohttp.ClientSession(cookies={"secretkey":self.secret_key}) as session:
async with session.get(f"{self.backend_url}/api/discord?discord_id={user.id}", ssl=False) as response:
steamid_response = await response.json()
if steamid_response != None:
try:
steamid_response = await response.json()
except:
raise NeedDiscordAuthOfSteam
if response.status == 200 and steamid_response != None:
self.discord2steam_cache[user.id] = steamid_response["steam64"]
else:
raise NeedDiscordAuthOfSteam

110
other_ext/register.py

@ -0,0 +1,110 @@
import discord
from ..exceptions import *
from ..bot import DiscordClient
from ..player import Player
class Extension:
auth_channel = 960796520247091201
default_role = 684828780040421388
def __init__(self, core: DiscordClient, enabled = True):
if not enabled: return None
@core.tree.command(name = "register", description = "Привязать дискорд к стиму")
@discord.app_commands.describe(profile=core.ANY_INPUT)
async def register(
interaction: discord.Interaction,
profile: str
):
await interaction.response.defer(thinking=True)
try:
steam64 = await core.GetSteam64OfDiscord(interaction.user)
return await interaction.followup.send('У тебя уже привязан профиль, стоит его для начала отвязать перед новой привязкой', ephemeral=True)
except:
pass
try:
profile = await core.GetPlayer(profile, "", True)
except:
return await interaction.followup.send("""
Невозможно в данный момент времени привязать такой профиль, причины:
1)Ты не играл на наших серверах, поэтому нельзя получить ид стима
2)Если ты кидал ссылку, то твоя ссылка является невалидной, попробуй другой формат
3)Сервисы стим устали
4)Сервисы факты13 устали
Попробуй по-другому или подожди...""", ephemeral=True)
profile_discord_id = await profile.GetDiscordId()
if profile_discord_id != 0:
return await interaction.followup.send(f'Профиль {profile.community_url} уже привязан к пользователю <@{profile_discord_id}>, если у тебя угнали профиль, напиши об этом <@142269939783434240>', ephemeral=True)
result = await profile.CreateDiscordId(interaction.user.id)
if not result:
return await interaction.followup.send('Кароч нельзя привязать профиль(((', ephemeral=True)
await self.final_stage(interaction.user)
return await interaction.followup.send(f'Ты крут!', ephemeral=True)
@core.tree.command(name = "unregister", description = "Отвязать профиль")
@discord.app_commands.describe()
async def unregister(
interaction: discord.Interaction
):
await interaction.response.defer(thinking=True)
steam64 = await core.GetSteam64OfDiscord(interaction.user)
player: Player = await core.GetPlayer(steam64, steam64, False)
await player.RemoveDiscordId(interaction.user.id)
await interaction.followup.send(f'Профиль отвязан, друг', ephemeral=True)
await self.remove_role(interaction.user.id)
return
@core.event
async def on_member_join(member):
if member.guild.id != core.main_server_id:
return
try:
core.GetSteam64OfDiscord(member, True)
self.final_stage(member)
return
except:
pass
auth_channel: discord.TextChannel = core.fetch_channel(self.auth_channel)
welcome_message = f"Привет <@{member.id}>!!!\n" + """
Добро пожаловать на оффициальный сервер дискорда факты 13!
Чтоб войти и получить роль для доступа ко всему, используй команду /register своим профилем стима или никнейм из тф.
Например: /register отдыхаем
Если все правильно, то этот канал для тебя исчезнет и ты получишь доступ ко всем остальным каналам.
> Так-же ты можешь прочитать местные правила <#734497550232453340> Если ты их видишь))
> Если не получается получить роль через бота пиши <@142269939783434240>"""
return await auth_channel.send(content=welcome_message, delete_after=120)
@core.event
async def on_message(message: discord.Message):
if message.guild.id != core.main_server_id:
return
if message.channel.id != self.auth_channel:
return
if message.author == core.user or message.author.id == 142269939783434240:
return
try:
await message.delete()
except Exception as err:
print(f"Cannot delete message from auth channel, error: {err}")
return
async def final_stage(self, user: discord.Member):
try:
await user.add_roles(discord.Role(id=self.default_role))
except Exception as err:
print(f"Cannot set role({self.default_role}) to {user}, error: {err}")
async def remove_role(self, user: discord.Member):
try:
await user.remove_roles(discord.Role(id=self.default_role))
except Exception as err:
print(f"Cannot remove role({self.default_role}) from {user}, error: {err}")

51
other_ext/url_checker.py

@ -0,0 +1,51 @@
from discord.ext import tasks
import discord
from ..bot import DiscordClient
import re
class Extension:
allow_domains = ["steamcommunity.com","s.team","tenor.com","tf2.pblr-nyk.pro", "pblr-nyk.pro", "youtu.be", "youtube.com"]
WEB_URL_REGEX = r"""(?i)\b((?:https?:(?:/{1,3}|[a-z0-9%])|[a-z0-9.\-]+[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)/)(?:[^\s()<>{}\[\]]+|\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\))+(?:\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])|(?:(?<!@)[a-z0-9]+(?:[.\-][a-z0-9]+)*[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)\b/?(?!@)))"""
allow_roles = [744928190895489074, 976924951447736453]
def __init__(self, core: DiscordClient):
@core.event
async def on_message(message: discord.Message):
if not message.guild:
return
if message.guild.id != core.main_server_id:
return
if message.author == core.user:
return
if not message.content:
return
for maybe_url in reversed(re.findall(self.WEB_URL_REGEX, message.content)):
for rid in self.allow_roles:
if message.author.get_role(rid):
return
sub_continue = False
for domain in self.allow_domains:
if domain in maybe_url[:26]:
sub_continue = True
break
if sub_continue:
continue
print(f"Found url from {message.author} with url {maybe_url}")
try:
await message.reply(f"{message.author.mention} ссылки кидать нельзя!\nhttps://tf2.pblr-nyk.pro/fekalis/nigger_maslo.gif", delete_after = 60)
except Exception as err:
print(f"Cannot send message about delete url, error: {err}")
try:
await message.delete()
except Exception as err:
print(f"Cannot delete message with url, error: {err}")
return
return

58
other_ext/webhook_helper.py

@ -0,0 +1,58 @@
from discord.ext import tasks
import discord
from ..bot import DiscordClient
class Extension:
core = None
track_guild_id = 0
track_channel_id = 712673654487515238
track_webhook_id = 740681080557797499
track_role_id = 744928190895489074
no_reason = {}
def __init__(self, core: DiscordClient):
self.core = core
self.track_guild_id = core.main_server_id
self.webhook_reaction_translate = {
"😘":(self.core.loaded_extensions['ban'], {"profile":"", "reason":"", "minutes":0, "requester":""}),
"🔨":(self.core.loaded_extensions['ban'], {"profile":"", "reason":"", "minutes":30, "requester":""}),
"⚒️":(self.core.loaded_extensions['ban'], {"profile":"", "reason":"", "minutes":120, "requester":""}),
"📨":(self.no_reason, {"requester":""}),
"🦵":"kick {reported_steam}",
"🔇":"mute {reported_steam}",
"😇":"unban {reported_steam}",
"hueglot:713139400028061767":"kick {original_steam}",
"🔍":"{reported_steam}",
"🔎":"{original_steam}"
}
@core.event
async def on_message(message: discord.Message):
if message.webhook_id != self.track_webhook_id:
return
for emoji in list(self.webhook_reaction_translate.keys()):
try:
message.add_reaction(emoji=emoji)
except Exception as err:
print(f"Cannot add reaction on webhook")
return
@core.event
async def on_reaction_add(reaction: discord.Reaction, user: discord.User):
if reaction.message.webhook_id != self.track_webhook_id:
return
if reaction.me:
return
if str(reaction.emoji) in self.webhook_reaction_translate.keys():
executor, kwargs = self.webhook_reaction_translate[str(reaction.emoji)]
if "requester" in kwargs:
kwargs["requester"] = user.id
async def null_reason(self, requester):
self.no_reason[requester] = "no reason"

43
player.py

@ -31,6 +31,10 @@ class Player:
def steam64(self):
return self.steamid.get('steam64', 0)
@property
def community_url(self):
return self.steamid.get('community_url', '')
@property
def embed(self) -> Embed:
if not self.current:
@ -248,6 +252,45 @@ class Player:
else:
return f"Репорт отправлен!"
#############
#discord sync
#############
async def GetDiscordId(self):
async with aiohttp.ClientSession(cookies={
"secretkey":os.getenv("BACKEND_SECRETKEY")}) as session:
async with session.get(f"{os.getenv('BACKEND_URL')}/api/discord/steam?steam64={self.steam64}", ssl = False) as response:
try:
discord_id = int(await response.text())
if response.status == 404:
raise Exception#код в ахуй
except:
discord_id = 0
return discord_id
async def CreateDiscordId(self, discord_id):
async with aiohttp.ClientSession(cookies={
"secretkey":os.getenv("BACKEND_SECRETKEY")}) as session:
async with session.post(f"{os.getenv('BACKEND_URL')}/api/discord?discord_id={discord_id}&steam_id={self.steam64}", ssl = False) as response:
result = await response.text()
if response.status == 406:
return False
elif response.status == 201:
return result
else:
return False
async def RemoveDiscordId(self, discord_id):
async with aiohttp.ClientSession(cookies={
"secretkey":os.getenv("BACKEND_SECRETKEY")}) as session:
async with session.delete(f"{os.getenv('BACKEND_URL')}/api/discord?discord_id={discord_id}", ssl = False) as response:
result = await response.text()
if response.status == 406:
return False
elif response.status == 200:
return result
else:
return False
def workshopmap2bsp(map_name):
return map_name.split('/')[-1:][0].split('.ugc')[0]

16
user_ext/profile.py

@ -1,7 +1,11 @@
import discord
from ..player import Player
from ..bot import DiscordClient
class Extension:
def __init__(self, core):
core: DiscordClient
def __init__(self, core: DiscordClient):
self.core = core
@core.tree.command(name = "profile", description = "Проверить профиль")
@discord.app_commands.describe(profile=core.ANY_INPUT)
async def check_profile(
@ -9,6 +13,10 @@ class Extension:
profile: str = ""
):
await interaction.response.defer(thinking=True)
steam64 = await core.GetSteam64OfDiscord(interaction.user)
player = await core.GetPlayer(profile, steam64) if profile else await core.GetPlayer(steam64, steam64)
return await interaction.followup.send(embed=player.embed, ephemeral=False)
player = self.__call__(profile, interaction.user)
return await interaction.followup.send(embed=player.embed, ephemeral=False)
async def __call__(self, profile:str, discord_user: discord.Member) -> Player:
steam64 = await self.core.GetSteam64OfDiscord(discord_user)
player = await self.core.GetPlayer(profile, steam64) if profile else await self.core.GetPlayer(steam64, steam64)
return player
Loading…
Cancel
Save