From 0849c1bdff971776c8e31a18bd1889f4b8bcbc98 Mon Sep 17 00:00:00 2001 From: Romain J Date: Sun, 22 Sep 2019 01:55:43 +0200 Subject: [PATCH] refactor(command|utility): rewrite iplocalise --- README.md | 13 +++- bot.py | 1 + cogs/basics.py | 35 +++++++++- cogs/utility.py | 78 +++++++++++++++++++++++ cogs/utils/__init__.py | 5 ++ extras/locales/en/LC_MESSAGES/basics.po | 6 ++ extras/locales/en/LC_MESSAGES/utility.mo | Bin 0 -> 444 bytes extras/locales/en/LC_MESSAGES/utility.po | 31 +++++++++ extras/locales/fr/LC_MESSAGES/basics.mo | Bin 873 -> 933 bytes extras/locales/fr/LC_MESSAGES/basics.po | 6 ++ extras/locales/fr/LC_MESSAGES/utility.mo | Bin 0 -> 693 bytes extras/locales/fr/LC_MESSAGES/utility.po | 31 +++++++++ 12 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 cogs/utility.py create mode 100644 extras/locales/en/LC_MESSAGES/utility.mo create mode 100644 extras/locales/en/LC_MESSAGES/utility.po create mode 100644 extras/locales/fr/LC_MESSAGES/utility.mo create mode 100644 extras/locales/fr/LC_MESSAGES/utility.po diff --git a/README.md b/README.md index db3d1bd..07717db 100644 --- a/README.md +++ b/README.md @@ -67,4 +67,15 @@ - [ ] ci setos - [ ] ci setcountry - [ ] ci online_edit `renamed`, cause : `website down` - - [ ] ci list \ No newline at end of file + - [ ] ci list + + --- + + # Cogs.utility commands + - [ ] clock + - [ ] clock * + - [ ] ytdiscover + - [x] iplocalise + - [ ] getheaders + - [ ] git + - [ ] quote \ No newline at end of file diff --git a/bot.py b/bot.py index c32d404..120e46c 100755 --- a/bot.py +++ b/bot.py @@ -24,6 +24,7 @@ log = logging.getLogger(__name__) l_extensions = ( 'cogs.admin', 'cogs.basics', + 'cogs.utility', 'cogs.logs', 'jishaku', ) diff --git a/cogs/basics.py b/cogs/basics.py index d293f46..3f43f06 100644 --- a/cogs/basics.py +++ b/cogs/basics.py @@ -1,4 +1,5 @@ import os +import pathlib import platform import time @@ -40,9 +41,30 @@ class Basics(commands.Cog): """---------------------------------------------------------------------""" + @staticmethod + def fetch_info(): + total = 0 + file_amount = 0 + ENV = "env" + + for path, _, files in os.walk("."): + for name in files: + file_dir = str(pathlib.PurePath(path, name)) + if not name.endswith(".py") or ENV in file_dir: + continue + file_amount += 1 + with open(file_dir, "r", encoding="utf-8") as file: + for line in file: + if not line.strip().startswith("#") or not line.strip(): + total += 1 + + return total, file_amount + @commands.command(name='info', aliases=['about']) async def _info(self, ctx: commands.Context): proc = psutil.Process() + lines, files = self.fetch_info() + with proc.oneshot(): mem = proc.memory_full_info() e = discord.Embed( @@ -93,7 +115,18 @@ class Basics(commands.Cog): ) e.add_field( - name=f"__{Texts('basics').get('Links')}__", + name=f"__:file_folder: {Texts('basics').get('Files')}__", + value=str(files), + inline=True + ) + e.add_field( + name=f"__¶ {Texts('basics').get('Lines')}__", + value=str(lines), + inline=True + ) + + e.add_field( + name=f"__:link: {Texts('basics').get('Links')}__", value="[tuxbot.gnous.eu](https://tuxbot.gnous.eu/) " "| [gnous.eu](https://gnous.eu/) " f"| [{Texts('basics').get('Invite')}](https://discordapp.com/oauth2/authorize?client_id=301062143942590465&scope=bot&permissions=268749888)", diff --git a/cogs/utility.py b/cogs/utility.py new file mode 100644 index 0000000..13a0ecd --- /dev/null +++ b/cogs/utility.py @@ -0,0 +1,78 @@ +import re + +import discord +from discord.ext import commands +from bot import TuxBot +import socket + +from .utils.lang import Texts + + +class Utility(commands.Cog): + + def __init__(self, bot: TuxBot): + self.bot = bot + + """---------------------------------------------------------------------""" + + async def fetch_api(self, ctx: commands.Context, ip, addr): + async with self.bot.session.get(f"http://ip-api.com/json/{ip}") as s: + response: dict = await s.json() + + if response.get('status') == 'success': + e = discord.Embed( + title=f"{Texts('utility').get('Information for')} " + f"``{addr}`` *`({response.get('query')})`*", + color=0x5858d7 + ) + + e.add_field( + name=Texts('utility').get('Belongs to :'), + value=response.get('org', 'N/A'), + inline=False + ) + + e.add_field( + name=Texts('utility').get('Is located at :'), + value=response.get('city', 'N/A'), + inline=True + ) + + e.add_field( + name="Region :", + value=f"{response.get('regionName', 'N/A')} " + f"({response.get('country', 'N/A')})", + inline=True + ) + + e.set_thumbnail( + url=f"https://www.countryflags.io/" + f"{response.get('countryCode')}/flat/64.png") + + await ctx.send(embed=e) + else: + await ctx.send( + content=f"{Texts('utility').get('info not available')}" + f"``{response.get('query')}``") + + @commands.command(name='iplocalise') + async def _iplocalise(self, ctx: commands.Context, addr, ip_type=''): + addr = re.sub(r'http(s?)://', '', addr) + addr = addr[:-1] if addr.endswith('/') else addr + + await ctx.trigger_typing() + + if ip_type in ('v6', 'ipv6'): + try: + ip = socket.getaddrinfo(addr, None, socket.AF_INET6)[1][4][0] + except socket.gaierror: + return await ctx.send( + Texts('utility').get('ipv6 not available')) + else: + ip = socket.gethostbyname(addr) + + await self.fetch_api(ctx, ip, addr) + + +def setup(bot: TuxBot): + bot.add_cog(Utility(bot)) diff --git a/cogs/utils/__init__.py b/cogs/utils/__init__.py index e69de29..caaa513 100755 --- a/cogs/utils/__init__.py +++ b/cogs/utils/__init__.py @@ -0,0 +1,5 @@ +from .checks import * +from .config import * +from .db import * +from .lang import * +from .version import * \ No newline at end of file diff --git a/extras/locales/en/LC_MESSAGES/basics.po b/extras/locales/en/LC_MESSAGES/basics.po index 194ccd3..89d6c26 100644 --- a/extras/locales/en/LC_MESSAGES/basics.po +++ b/extras/locales/en/LC_MESSAGES/basics.po @@ -42,6 +42,12 @@ msgstr "" msgid "Links" msgstr "" +msgid "Files" +msgstr "" + +msgid "Lines" +msgstr "" + msgid "Invite" msgstr "" diff --git a/extras/locales/en/LC_MESSAGES/utility.mo b/extras/locales/en/LC_MESSAGES/utility.mo new file mode 100644 index 0000000000000000000000000000000000000000..5afe2611a5017f86254e6ee633d6b55f58954979 GIT binary patch literal 444 zcmY+A-%i3X6vn6VYL{GlaqgjWBTAqGVM4*h{mU36dRJJTHM3i~y119{&e!u<>=G07 zq`#c6>6g>5{q9|S8M@9jmzqJh`BPJ^dzx9tnPJ4bBh9DQ2CteCh*AM@FYN4S_vLwq6vPag?H^WK`)>w4#b(7~0MR*?Nt^nPB_6Zx3x- z8&Sd<-c9-SJrXxTL4;N=+($U@`cF|vOrO>YrIL!epsJJ@);r&a&idg=TcynBX5aV?-pvrMwO1}*=6F>nE1+>9*a2MFqYEC2ui literal 0 HcmV?d00001 diff --git a/extras/locales/en/LC_MESSAGES/utility.po b/extras/locales/en/LC_MESSAGES/utility.po new file mode 100644 index 0000000..d72dedf --- /dev/null +++ b/extras/locales/en/LC_MESSAGES/utility.po @@ -0,0 +1,31 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR ORGANIZATION +# FIRST AUTHOR , YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"POT-Creation-Date: 2019-09-08 19:04+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: pygettext.py 1.5\n" + + +msgid "ipv6 not available" +msgstr "Error, this address is not available in IPv6." + +msgid "Information for" +msgstr "" + +msgid "Belongs to :" +msgstr "" + +msgid "Is located at :" +msgstr "" + +msgid "info not available" +msgstr "" \ No newline at end of file diff --git a/extras/locales/fr/LC_MESSAGES/basics.mo b/extras/locales/fr/LC_MESSAGES/basics.mo index 23ac26ed2ec93d6cadf6f6f86429b31a59e97c12..f594e3b48f58cc9e4afff93239b7a51ca9432448 100644 GIT binary patch delta 299 zcmaFKwv@g8o)F7a1|Z-AVi_Rr0dbJPA0Qhf_ZNtHK$ww%fg4Cm0%=wttqPeg=kmh9)2bq+t?}wguvaKr8^{-)Dko_yD9q z^8bJ|2ax7vW?&Ep(qcfG3rOn$X%Qf838X;|@B`8yhlK)ZK_DFsq=kTVE|lK@w8f{KyT*Ww9jM-{( HII|xBY$+GJ diff --git a/extras/locales/fr/LC_MESSAGES/basics.po b/extras/locales/fr/LC_MESSAGES/basics.po index a345d05..429badb 100644 --- a/extras/locales/fr/LC_MESSAGES/basics.po +++ b/extras/locales/fr/LC_MESSAGES/basics.po @@ -42,6 +42,12 @@ msgstr "Nombre de membres" msgid "Links" msgstr "Liens" +msgid "Files" +msgstr "Fichiers" + +msgid "Lines" +msgstr "Lignes" + msgid "Invite" msgstr "Invitation" diff --git a/extras/locales/fr/LC_MESSAGES/utility.mo b/extras/locales/fr/LC_MESSAGES/utility.mo new file mode 100644 index 0000000000000000000000000000000000000000..f5d6167b7ccbf3c6ed1295dc17a0ceed6b997922 GIT binary patch literal 693 zcmZ9J&2AGh5XTKd99DdYo)MZ`55-|qph~xG)J?JtD|VA8AC=(dWT%VK#g$(;*GEl{mE>4##>jux|a*I zJW&f>NL5DAnoA$_QK7AWDer6|Fq;y6ovdLt&}=*pSZs{2UQ#Le@%8#l92mW_Y=YpV zU*Vs4VlA%hV~|Uuos%>ZD0cOndX*LmbWjg%J5&d5WnocQ1W{OY4j~<}zbUQ<)Gj&? TMmQ*>Gg@7yMnw, YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"POT-Creation-Date: 2019-09-08 19:04+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: pygettext.py 1.5\n" + + +msgid "ipv6 not available" +msgstr "Erreur, cette adresse n'est pas disponible en IPv6." + +msgid "Information for" +msgstr "Informations pour" + +msgid "Belongs to :" +msgstr "Appartient à :" + +msgid "Is located at :" +msgstr "Se situe à :" + +msgid "info not available" +msgstr "Erreur, impossible d'obtenir des informations sur cette adresse IP" \ No newline at end of file