2020-01-04 02:46:12 +01:00
|
|
|
|
# Created by romain at 04/01/2020
|
|
|
|
|
|
2019-12-30 00:48:11 +01:00
|
|
|
|
import logging
|
2019-09-15 02:31:31 +02:00
|
|
|
|
import os
|
2019-09-22 01:55:43 +02:00
|
|
|
|
import pathlib
|
2019-09-15 01:25:32 +02:00
|
|
|
|
import platform
|
2020-01-04 02:46:12 +01:00
|
|
|
|
import re
|
|
|
|
|
import socket
|
2019-09-15 01:25:32 +02:00
|
|
|
|
import time
|
2020-01-04 02:46:12 +01:00
|
|
|
|
from socket import AF_INET6
|
2019-09-15 01:25:32 +02:00
|
|
|
|
|
2020-01-04 02:46:12 +01:00
|
|
|
|
import aiohttp
|
2019-09-15 01:25:32 +02:00
|
|
|
|
import discord
|
|
|
|
|
import humanize
|
|
|
|
|
import psutil
|
|
|
|
|
from discord.ext import commands
|
2020-01-04 02:46:12 +01:00
|
|
|
|
from tcp_latency import measure_latency
|
2019-09-15 01:25:32 +02:00
|
|
|
|
|
|
|
|
|
from bot import TuxBot
|
2020-01-04 02:46:12 +01:00
|
|
|
|
from utils import Texts
|
|
|
|
|
from utils import commandExtra
|
2019-09-15 01:25:32 +02:00
|
|
|
|
|
2019-12-30 00:48:11 +01:00
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
2019-09-15 01:25:32 +02:00
|
|
|
|
|
2020-01-04 02:46:12 +01:00
|
|
|
|
class Useful(commands.Cog):
|
2019-09-15 01:25:32 +02:00
|
|
|
|
|
|
|
|
|
def __init__(self, bot: TuxBot):
|
|
|
|
|
self.bot = bot
|
2020-01-04 02:46:12 +01:00
|
|
|
|
self.icon = ":toolbox:"
|
|
|
|
|
self.big_icon = "https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/twitter/233/toolbox_1f9f0.png"
|
2019-09-15 01:25:32 +02:00
|
|
|
|
|
2019-09-15 02:31:31 +02:00
|
|
|
|
@staticmethod
|
|
|
|
|
def _latest_commits():
|
|
|
|
|
cmd = 'git log -n 3 -s --format="[\`%h\`](https://git.gnous.eu/gnouseu/tuxbot-bot/commits/%H) %s (%cr)"'
|
|
|
|
|
|
|
|
|
|
return os.popen(cmd).read().strip()
|
|
|
|
|
|
2019-12-17 22:41:54 +01:00
|
|
|
|
###########################################################################
|
2019-09-15 01:25:32 +02:00
|
|
|
|
|
2020-01-04 21:16:37 +01:00
|
|
|
|
@commandExtra(name='iplocalise', category='useful',
|
|
|
|
|
description=Texts('useful_help').get('_iplocalise'))
|
2020-01-04 02:46:12 +01:00
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if ip_type in ('v6', 'ipv6'):
|
|
|
|
|
try:
|
|
|
|
|
ip = socket.getaddrinfo(addr, None, AF_INET6)[1][4][0]
|
|
|
|
|
except socket.gaierror:
|
|
|
|
|
return await ctx.send(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
Texts('useful', ctx).get('ipv6 not available'))
|
2020-01-04 02:46:12 +01:00
|
|
|
|
else:
|
|
|
|
|
ip = socket.gethostbyname(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(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
title=f"{Texts('useful', ctx).get('Information for')}"
|
2020-01-04 02:46:12 +01:00
|
|
|
|
f" ``{addr}`` *`({response.get('query')})`*",
|
|
|
|
|
color=0x5858d7
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
e.add_field(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
name=Texts('useful', ctx).get('Belongs to :'),
|
2020-01-04 02:46:12 +01:00
|
|
|
|
value=response.get('org', 'N/A'),
|
|
|
|
|
inline=False
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
e.add_field(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
name=Texts('useful', ctx).get('Is located at :'),
|
2020-01-04 02:46:12 +01:00
|
|
|
|
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(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
content=f"{Texts('useful', ctx).get('info not available')}"
|
2020-01-04 02:46:12 +01:00
|
|
|
|
f"``{response.get('query')}``")
|
|
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
await ctx.send(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
f"{Texts('useful', ctx).get('Cannot connect to host')} {addr}"
|
2020-01-04 02:46:12 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
###########################################################################
|
|
|
|
|
|
2020-01-04 21:16:37 +01:00
|
|
|
|
@commandExtra(name='getheaders', category='useful',
|
|
|
|
|
description=Texts('useful_help').get('_getheaders'))
|
2020-01-04 02:46:12 +01:00
|
|
|
|
async def _getheaders(self, ctx: commands.Context, addr: str):
|
|
|
|
|
if (addr.startswith('http') or addr.startswith('ftp')) is not True:
|
|
|
|
|
addr = f"http://{addr}"
|
|
|
|
|
|
|
|
|
|
await ctx.trigger_typing()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
async with self.bot.session.get(addr) as s:
|
|
|
|
|
e = discord.Embed(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
title=f"{Texts('useful', ctx).get('Headers of')} {addr}",
|
2020-01-04 02:46:12 +01:00
|
|
|
|
color=0xd75858
|
|
|
|
|
)
|
|
|
|
|
e.add_field(name="Status", value=s.status, inline=True)
|
|
|
|
|
e.set_thumbnail(url=f"https://http.cat/{s.status}")
|
|
|
|
|
|
|
|
|
|
headers = dict(s.headers.items())
|
|
|
|
|
headers.pop('Set-Cookie', headers)
|
|
|
|
|
|
|
|
|
|
for key, value in headers.items():
|
|
|
|
|
e.add_field(name=key, value=value, inline=True)
|
|
|
|
|
await ctx.send(embed=e)
|
|
|
|
|
|
|
|
|
|
except aiohttp.client_exceptions.ClientError:
|
|
|
|
|
await ctx.send(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
f"{Texts('useful', ctx).get('Cannot connect to host')} {addr}"
|
2020-01-04 02:46:12 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
###########################################################################
|
|
|
|
|
|
|
|
|
|
@commandExtra(name='git', aliases=['sources', 'source', 'github'],
|
2020-01-04 21:16:37 +01:00
|
|
|
|
category='useful',
|
|
|
|
|
description=Texts('useful_help').get('_git'))
|
2020-01-04 02:46:12 +01:00
|
|
|
|
async def _git(self, ctx):
|
|
|
|
|
e = discord.Embed(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
title=Texts('useful', ctx).get('git repo'),
|
|
|
|
|
description=Texts('useful', ctx).get('git text'),
|
2020-01-04 02:46:12 +01:00
|
|
|
|
colour=0xE9D460
|
|
|
|
|
)
|
|
|
|
|
e.set_author(
|
|
|
|
|
name='Gnous',
|
|
|
|
|
icon_url="https://cdn.gnous.eu/logo1.png"
|
|
|
|
|
)
|
|
|
|
|
await ctx.send(embed=e)
|
|
|
|
|
|
|
|
|
|
###########################################################################
|
|
|
|
|
|
2020-01-04 21:16:37 +01:00
|
|
|
|
@commandExtra(name='quote', category='useful',
|
|
|
|
|
description=Texts('useful_help').get('_quote'))
|
2020-01-04 02:46:12 +01:00
|
|
|
|
async def _quote(self, ctx, message_id: discord.Message):
|
|
|
|
|
e = discord.Embed(
|
|
|
|
|
colour=message_id.author.colour,
|
|
|
|
|
description=message_id.clean_content,
|
|
|
|
|
timestamp=message_id.created_at
|
|
|
|
|
)
|
|
|
|
|
e.set_author(
|
|
|
|
|
name=message_id.author.display_name,
|
|
|
|
|
icon_url=message_id.author.avatar_url_as(format="jpg")
|
|
|
|
|
)
|
|
|
|
|
if len(message_id.attachments) >= 1:
|
|
|
|
|
e.set_image(url=message_id.attachments[0].url)
|
|
|
|
|
|
|
|
|
|
e.add_field(name="**Original**",
|
|
|
|
|
value=f"[Go!]({message_id.jump_url})")
|
|
|
|
|
e.set_footer(text="#" + message_id.channel.name)
|
|
|
|
|
|
|
|
|
|
await ctx.send(embed=e)
|
|
|
|
|
|
|
|
|
|
###########################################################################
|
|
|
|
|
|
2020-01-04 21:16:37 +01:00
|
|
|
|
@commandExtra(name='ping', category='useful',
|
|
|
|
|
description=Texts('useful_help').get('_ping'))
|
2019-09-15 01:25:32 +02:00
|
|
|
|
async def _ping(self, ctx: commands.Context):
|
|
|
|
|
start = time.perf_counter()
|
|
|
|
|
await ctx.trigger_typing()
|
|
|
|
|
end = time.perf_counter()
|
|
|
|
|
|
|
|
|
|
latency = round(self.bot.latency * 1000, 2)
|
|
|
|
|
typing = round((end - start) * 1000, 2)
|
2020-01-04 21:16:37 +01:00
|
|
|
|
discordapp = measure_latency(host='discordapp.com', wait=0)[0]
|
2019-09-15 01:25:32 +02:00
|
|
|
|
|
|
|
|
|
e = discord.Embed(title='Ping', color=discord.Color.teal())
|
|
|
|
|
e.add_field(name='Websocket', value=f'{latency}ms')
|
|
|
|
|
e.add_field(name='Typing', value=f'{typing}ms')
|
2019-10-06 23:49:00 +02:00
|
|
|
|
e.add_field(name='discordapp.com', value=f'{discordapp}ms')
|
2019-09-15 01:25:32 +02:00
|
|
|
|
await ctx.send(embed=e)
|
|
|
|
|
|
2019-12-17 22:41:54 +01:00
|
|
|
|
###########################################################################
|
2019-09-15 01:25:32 +02:00
|
|
|
|
|
2019-09-22 01:55:43 +02:00
|
|
|
|
@staticmethod
|
|
|
|
|
def fetch_info():
|
2020-01-04 21:16:37 +01:00
|
|
|
|
total_lines = 0
|
|
|
|
|
total_python_lines = 0
|
2019-09-22 01:55:43 +02:00
|
|
|
|
file_amount = 0
|
2020-01-04 21:16:37 +01:00
|
|
|
|
python_file_amount = 0
|
2019-09-22 01:55:43 +02:00
|
|
|
|
ENV = "env"
|
|
|
|
|
|
|
|
|
|
for path, _, files in os.walk("."):
|
|
|
|
|
for name in files:
|
|
|
|
|
file_dir = str(pathlib.PurePath(path, name))
|
2020-01-04 21:16:37 +01:00
|
|
|
|
if (
|
|
|
|
|
not name.endswith(".py")
|
|
|
|
|
and not name.endswith(".po")
|
|
|
|
|
and not name.endswith(".json")
|
|
|
|
|
) or ENV in file_dir:
|
2019-09-22 01:55:43 +02:00
|
|
|
|
continue
|
|
|
|
|
file_amount += 1
|
2020-01-04 21:16:37 +01:00
|
|
|
|
python_file_amount += 1 if name.endswith(".py") else 0
|
2019-09-22 01:55:43 +02:00
|
|
|
|
with open(file_dir, "r", encoding="utf-8") as file:
|
|
|
|
|
for line in file:
|
2019-10-06 01:49:30 +02:00
|
|
|
|
if not line.strip().startswith("#") \
|
|
|
|
|
or not line.strip():
|
2020-01-04 21:16:37 +01:00
|
|
|
|
total_lines += 1
|
|
|
|
|
total_python_lines += 1 if name.endswith(".py") \
|
|
|
|
|
else 0
|
2019-09-22 01:55:43 +02:00
|
|
|
|
|
2020-01-04 21:16:37 +01:00
|
|
|
|
return (file_amount, total_lines), (python_file_amount, total_python_lines)
|
2019-09-22 01:55:43 +02:00
|
|
|
|
|
2020-01-04 21:16:37 +01:00
|
|
|
|
@commandExtra(name='info', aliases=['about'], category='useful',
|
|
|
|
|
description=Texts('useful_help').get('_info'))
|
2019-09-15 01:25:32 +02:00
|
|
|
|
async def _info(self, ctx: commands.Context):
|
|
|
|
|
proc = psutil.Process()
|
2020-01-04 21:16:37 +01:00
|
|
|
|
total, python = self.fetch_info()
|
2019-09-22 01:55:43 +02:00
|
|
|
|
|
2019-09-15 01:25:32 +02:00
|
|
|
|
with proc.oneshot():
|
|
|
|
|
mem = proc.memory_full_info()
|
|
|
|
|
e = discord.Embed(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
title=Texts('useful', ctx).get('Information about TuxBot'),
|
2019-09-15 01:25:32 +02:00
|
|
|
|
color=0x89C4F9)
|
2019-09-15 02:31:31 +02:00
|
|
|
|
|
|
|
|
|
e.add_field(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
name=f"__{Texts('useful', ctx).get('Latest changes')}__",
|
2019-09-15 02:31:31 +02:00
|
|
|
|
value=self._latest_commits(),
|
|
|
|
|
inline=False)
|
|
|
|
|
|
2019-09-15 01:25:32 +02:00
|
|
|
|
e.add_field(
|
|
|
|
|
name=f"__:busts_in_silhouette: "
|
2020-01-04 21:16:37 +01:00
|
|
|
|
f"{Texts('useful', ctx).get('Development')}__",
|
2019-09-15 01:25:32 +02:00
|
|
|
|
value=f"**Romain#5117:** [git](https://git.gnous.eu/Romain)\n"
|
|
|
|
|
f"**Outout#4039:** [git](https://git.gnous.eu/mael)\n",
|
|
|
|
|
inline=True
|
|
|
|
|
)
|
|
|
|
|
e.add_field(
|
|
|
|
|
name="__<:python:596577462335307777> Python__",
|
|
|
|
|
value=f"**python** `{platform.python_version()}`\n"
|
|
|
|
|
f"**discord.py** `{discord.__version__}`",
|
|
|
|
|
inline=True
|
|
|
|
|
)
|
|
|
|
|
e.add_field(
|
|
|
|
|
name="__:gear: Usage__",
|
|
|
|
|
value=f"**{humanize.naturalsize(mem.rss)}** "
|
2020-01-04 21:16:37 +01:00
|
|
|
|
f"{Texts('useful', ctx).get('physical memory')}\n"
|
2019-09-15 01:25:32 +02:00
|
|
|
|
f"**{humanize.naturalsize(mem.vms)}** "
|
2020-01-04 21:16:37 +01:00
|
|
|
|
f"{Texts('useful', ctx).get('virtual memory')}\n",
|
2019-09-15 01:25:32 +02:00
|
|
|
|
inline=True
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
e.add_field(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
name=f"__{Texts('useful', ctx).get('Servers count')}__",
|
2019-09-15 01:25:32 +02:00
|
|
|
|
value=str(len(self.bot.guilds)),
|
|
|
|
|
inline=True
|
|
|
|
|
)
|
|
|
|
|
e.add_field(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
name=f"__{Texts('useful', ctx).get('Channels count')}__",
|
2019-09-15 01:25:32 +02:00
|
|
|
|
value=str(len([_ for _ in self.bot.get_all_channels()])),
|
|
|
|
|
inline=True
|
|
|
|
|
)
|
|
|
|
|
e.add_field(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
name=f"__{Texts('useful', ctx).get('Members count')}__",
|
2019-09-15 01:25:32 +02:00
|
|
|
|
value=str(len([_ for _ in self.bot.get_all_members()])),
|
|
|
|
|
inline=True
|
|
|
|
|
)
|
|
|
|
|
|
2019-09-15 02:31:31 +02:00
|
|
|
|
e.add_field(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
name=f"__:file_folder: {Texts('useful', ctx).get('Files')}__",
|
|
|
|
|
value=f"{total[0]} *({python[0]} <:python:596577462335307777>)*",
|
2019-09-22 01:55:43 +02:00
|
|
|
|
inline=True
|
|
|
|
|
)
|
|
|
|
|
e.add_field(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
name=f"__¶ {Texts('useful', ctx).get('Lines')}__",
|
|
|
|
|
value=f"{total[1]} *({python[1]} <:python:596577462335307777>)*",
|
2019-09-22 01:55:43 +02:00
|
|
|
|
inline=True
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
e.add_field(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
name=f"__:link: {Texts('useful', ctx).get('Links')}__",
|
2019-09-15 02:31:31 +02:00
|
|
|
|
value="[tuxbot.gnous.eu](https://tuxbot.gnous.eu/) "
|
|
|
|
|
"| [gnous.eu](https://gnous.eu/) "
|
2019-12-16 19:36:55 +01:00
|
|
|
|
"| [git](https://git.gnous.eu/gnouseu/tuxbot-bot) "
|
2020-01-04 21:16:37 +01:00
|
|
|
|
"| [status](https://status.gnous.eu/check/154250) "
|
|
|
|
|
f"| [{Texts('useful', ctx).get('Invite')}](https://discordapp.com/oauth2/authorize?client_id=301062143942590465&scope=bot&permissions=268749888)",
|
2019-09-15 02:31:31 +02:00
|
|
|
|
inline=False
|
|
|
|
|
)
|
|
|
|
|
|
2019-10-26 23:19:47 +02:00
|
|
|
|
e.set_footer(text=f'version: {self.bot.version} '
|
|
|
|
|
f'• prefix: {ctx.prefix}')
|
2019-09-15 01:25:32 +02:00
|
|
|
|
|
|
|
|
|
await ctx.send(embed=e)
|
|
|
|
|
|
2019-12-17 22:41:54 +01:00
|
|
|
|
###########################################################################
|
2019-09-15 02:31:31 +02:00
|
|
|
|
|
2019-12-16 23:06:25 +01:00
|
|
|
|
@commandExtra(name='credits', aliases=['contributors', 'authors'],
|
2020-01-04 21:16:37 +01:00
|
|
|
|
category='useful',
|
|
|
|
|
description=Texts('useful_help').get('_credits'))
|
2019-09-15 02:31:31 +02:00
|
|
|
|
async def _credits(self, ctx: commands.Context):
|
|
|
|
|
e = discord.Embed(
|
2020-01-04 21:16:37 +01:00
|
|
|
|
title=Texts('useful', ctx).get('Contributors'),
|
2019-09-15 02:56:41 +02:00
|
|
|
|
color=0x36393f
|
2019-09-15 02:31:31 +02:00
|
|
|
|
)
|
|
|
|
|
|
2019-09-15 02:56:41 +02:00
|
|
|
|
e.add_field(
|
|
|
|
|
name="**Outout#4039** ",
|
|
|
|
|
value="• https://git.gnous.eu/mael ⠀\n"
|
|
|
|
|
"• mael@gnous.eu\n"
|
|
|
|
|
"• [@outoutxyz](https://twitter.com/outouxyz)",
|
|
|
|
|
inline=True
|
|
|
|
|
)
|
|
|
|
|
e.add_field(
|
|
|
|
|
name="**Romain#5117** ",
|
|
|
|
|
value="• https://git.gnous.eu/Romain\n"
|
|
|
|
|
"• romain@gnous.eu",
|
|
|
|
|
inline=True
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await ctx.send(embed=e)
|
|
|
|
|
|
2019-09-15 01:25:32 +02:00
|
|
|
|
|
|
|
|
|
def setup(bot: TuxBot):
|
2020-01-04 02:46:12 +01:00
|
|
|
|
bot.add_cog(Useful(bot))
|