diff --git a/.idea/dictionaries/romain.xml b/.idea/dictionaries/romain.xml
new file mode 100644
index 0000000..f471c50
--- /dev/null
+++ b/.idea/dictionaries/romain.xml
@@ -0,0 +1,11 @@
+
+
+
+ ipinfo
+ iplocalise
+ localiseip
+ postgresql
+ splt
+
+
+
\ No newline at end of file
diff --git a/.idea/discord.xml b/.idea/discord.xml
index 59b11d1..a04e4e5 100644
--- a/.idea/discord.xml
+++ b/.idea/discord.xml
@@ -1,7 +1,7 @@
-
+
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index f898223..3fe55d9 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,14 +1,18 @@
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
@@ -38,34 +42,34 @@
-
+
-
+
-
-
-
-
+
+
+
+
-
+
1589922546510
- 1589991138014
+ 1590880224598
@@ -80,12 +84,49 @@
-
+
+
+
+
+
+
+
+
+ https://trello.com/c/lpMrzLET/17-ping
+
+
+ 1590880232278
+
+
+
+
+
+
+ https://trello.com/c/A5f7QCk2/11-iplocalise
+
+
+ 1590880232278
+
+
+
-
+
+
+
+
+
+
+
+
@@ -103,22 +144,26 @@
-
+
-
+
+
+
+
+
-
+
-
-
+
+
-
+
@@ -131,10 +176,10 @@
-
+
-
+
@@ -147,9 +192,20 @@
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app.py b/app.py
index 3239aae..01ed3e0 100644
--- a/app.py
+++ b/app.py
@@ -19,6 +19,7 @@ l_extensions: List[str] = [
"jishaku",
"cogs.Logs",
"cogs.Images",
+ "cogs.Network",
"cogs.Useless",
]
diff --git a/cogs/Network.py b/cogs/Network.py
new file mode 100644
index 0000000..b4ee2ce
--- /dev/null
+++ b/cogs/Network.py
@@ -0,0 +1,110 @@
+import logging
+import socket
+import ipinfo
+import discord
+
+from discord.ext import commands, flags
+from ipwhois import Net
+from ipwhois.asn import IPASN
+from ipinfo.exceptions import RequestQuotaExceededError
+from requests.exceptions import HTTPError
+
+from app import TuxBot
+from utils.functions.extra import ContextPlus, command_extra
+
+log = logging.getLogger(__name__)
+
+
+class Network(commands.Cog, name="Useless"):
+ def __init__(self, bot: TuxBot):
+ self.bot = bot
+
+ @flags.add_flag("-i", "--ip", type=str, default='v4',
+ choices=['v4', '4', 'v6', '6'])
+ @command_extra(name="iplocalise", aliases=['localiseip'])
+ @commands.cooldown(1, 5, commands.BucketType.user)
+ async def _iplocalise(self, ctx: ContextPlus, target: str, **passed_flags):
+ loading = await ctx.send(
+ "_Récupération des informations..._", deletable=False
+ )
+
+ def get_hostname(dtl, tgt):
+ try:
+ return dtl.hostname
+ except AttributeError:
+ try:
+ return socket.gethostbyaddr(tgt)[0]
+ except (ValueError, socket.herror):
+ return 'N/A'
+
+ ip_type = passed_flags.get('ip')
+ target_copy = target
+
+ # clean https://, last /, ...
+ spltTgt = target.split("://")
+ target = spltTgt[
+ (0, 1)[len(spltTgt) > 1]
+ ].split("?")[0].split('/')[0].split(':')[0].lower()
+
+ try:
+ target = socket.getaddrinfo(
+ target, None,
+ socket.AF_INET if ip_type in ['v4', '4'] else socket.AF_INET6
+ )[1][4][0]
+ except socket.gaierror:
+ return \
+ await ctx.send("Erreur, cette adresse n'est pas disponible.")
+
+ net = Net(target)
+ obj = IPASN(net)
+ ip_info = obj.lookup()
+
+ try:
+ handler = ipinfo.getHandler(self.bot.config.ipinfo)
+ details = handler.getDetails(target)
+ api_result = True
+ except (RequestQuotaExceededError, HTTPError):
+ details = None
+ api_result = False
+
+ if api_result:
+ belongs = f"{details.org}"
+
+ osm = f"https://www.openstreetmap.org/" \
+ f"?mlat={details.latitude}" \
+ f"&mlon={details.longitude}" \
+ f"#map=5/{details.latitude}/{details.longitude}" \
+ f"&layers=H"
+
+ region = f"[{details.city} - {details.region} " \
+ f"({details.country})]({osm})"
+ flag = f"https://www.countryflags.io/" \
+ f"{details.country}/shiny/64.png"
+ else:
+ belongs = f"{ip_info['asn_description']} (AS{ip_info['asn']})"
+ region = f"{ip_info['asn_country_code']}"
+ flag = f"https://www.countryflags.io/" \
+ f"{ip_info['asn_country_code']}/shiny/64.png"
+
+ e = discord.Embed(
+ title=f"**Information sur __{target_copy}__ :**"
+ f" `{target}`",
+ color=0x5858d7
+ )
+
+ e.add_field(name="Appartient à :", value=belongs)
+ e.add_field(name="RIR :", value=f"{ip_info['asn_registry']}")
+ e.add_field(name="Region :", value=region)
+
+ e.add_field(name="Nom de l'hôte :",
+ value=get_hostname(details, target), inline=False)
+
+ e.set_thumbnail(url=flag)
+
+ await loading.delete()
+ await ctx.send(embed=e)
+
+
+def setup(bot: TuxBot):
+ cog = Network(bot)
+ bot.add_cog(cog)
diff --git a/configs/bot/protected.py b/configs/bot/protected.py
index 073705f..274a5ed 100644
--- a/configs/bot/protected.py
+++ b/configs/bot/protected.py
@@ -1,8 +1,9 @@
-from .settings import token, postgresql, logs
+from .settings import token, postgresql, logs, ipinfo
protected = [
token, str(list(token)),
postgresql, str(list(postgresql)),
- *[channel.get('webhook').get('token') for channel in logs.values()]
+ *[channel.get('webhook').get('token') for channel in logs.values()],
+ ipinfo
]
diff --git a/configs/bot/settings.py.example b/configs/bot/settings.py.example
index 626785d..20efa53 100644
--- a/configs/bot/settings.py.example
+++ b/configs/bot/settings.py.example
@@ -42,3 +42,6 @@ logs = {
}
postgresql = 'postgres://tuxbot:tuxbot@localhost:5432/tuxbot-rewrite'
+
+# https://ipinfo.io/
+ipfinfo = ''
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index 5c1a15e..177da83 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -24,3 +24,5 @@ six==1.14.0
typing-extensions==3.7.4.2
websockets==8.1
yarl==1.4.2
+ipinfo==3.0.0
+ipwhois==1.1.0
\ No newline at end of file
diff --git a/utils/functions/extra.py b/utils/functions/extra.py
index 5c4dbf5..25d5a87 100644
--- a/utils/functions/extra.py
+++ b/utils/functions/extra.py
@@ -43,7 +43,8 @@ class ContextPlus(commands.Context):
ast.literal_eval(embed)
)
- if self.command.deletable:
+ if (hasattr(self.command, 'deletable') and self.command.deletable) \
+ and kwargs.pop('deletable', True):
message = await super().send(content, *args, **kwargs)
await message.add_reaction('🗑')
@@ -55,13 +56,14 @@ class ContextPlus(commands.Context):
try:
await self.bot.wait_for(
'reaction_add',
- timeout=120.0,
+ timeout=60.0,
check=check
)
except asyncio.TimeoutError:
await message.remove_reaction('🗑', self.bot.user)
else:
await message.delete()
+ return message
else:
return await super().send(content, *args, **kwargs)