mirror of https://github.com/sgoudham/Enso-Bot.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
246 lines
9.8 KiB
Python
246 lines
9.8 KiB
Python
5 years ago
|
import datetime
|
||
5 years ago
|
import random
|
||
|
import string
|
||
5 years ago
|
from asyncio.subprocess import Process
|
||
|
from platform import python_version
|
||
|
from time import time
|
||
5 years ago
|
from typing import Optional
|
||
|
|
||
5 years ago
|
from discord import Embed
|
||
|
from discord import Member, Colour
|
||
|
from discord import __version__ as discord_version
|
||
5 years ago
|
from discord.ext import commands
|
||
5 years ago
|
from discord.ext.commands import BucketType, cooldown
|
||
|
from discord.ext.commands import command
|
||
|
from psutil import Process, virtual_memory
|
||
5 years ago
|
|
||
5 years ago
|
from settings import colour_list, enso_embedmod_colours
|
||
5 years ago
|
|
||
5 years ago
|
# Using forzenset
|
||
5 years ago
|
# Permissions to filter through
|
||
5 years ago
|
Perms = frozenset(
|
||
5 years ago
|
{
|
||
|
"create instant invite",
|
||
|
"add reactions",
|
||
|
"view audit log",
|
||
|
"priority speaker",
|
||
|
"stream",
|
||
|
"read messages",
|
||
|
"send messages",
|
||
|
"send tts messages",
|
||
|
"embed links",
|
||
|
"attach links",
|
||
|
"read message history",
|
||
|
"external emojis",
|
||
|
"view guild insights",
|
||
|
"connect",
|
||
|
"speak",
|
||
|
"use voice activation",
|
||
|
"change nickname"
|
||
|
}
|
||
5 years ago
|
)
|
||
5 years ago
|
|
||
5 years ago
|
|
||
5 years ago
|
# Method to detect which permissions to filter out
|
||
5 years ago
|
def DetectPermissions(message, fset):
|
||
5 years ago
|
# Split the message individual permissions
|
||
5 years ago
|
message = message.split(",")
|
||
|
|
||
5 years ago
|
# Filter the permission out if it's in the frozenset
|
||
|
filtered = filter(lambda perm: perm not in fset, message)
|
||
5 years ago
|
return ", ".join(filtered)
|
||
|
|
||
5 years ago
|
|
||
5 years ago
|
class Info(commands.Cog):
|
||
5 years ago
|
def __init__(self, bot):
|
||
|
self.bot = bot
|
||
|
|
||
5 years ago
|
@command(name="userinfo", aliases=["ui"])
|
||
5 years ago
|
@cooldown(1, 5, BucketType.user)
|
||
5 years ago
|
async def user_info(self, ctx, target: Optional[Member]):
|
||
5 years ago
|
"""Allow the users to see information about them relating to the guild"""
|
||
5 years ago
|
|
||
5 years ago
|
# If a target has been specified, set them as the user
|
||
|
if target:
|
||
|
target = target
|
||
|
# If no target has been specified, choose the author
|
||
|
else:
|
||
|
target = ctx.author
|
||
|
|
||
|
# Get the member avatar
|
||
|
userAvatar = target.avatar_url
|
||
|
|
||
5 years ago
|
# Store all the roles that the user has
|
||
|
# (Skipping the first element as it's always going to be @everyone)
|
||
|
roles = f"{' '.join(map(str, (role.mention for role in target.roles[1:])))}"
|
||
5 years ago
|
|
||
5 years ago
|
# Returns the permissions that the user has within the guild
|
||
5 years ago
|
filtered = filter(lambda x: x[1], target.guild_permissions)
|
||
5 years ago
|
# Replace all "_" with " " in each item and join them together
|
||
5 years ago
|
permission = ",".join(map(lambda x: x[0].replace("_", " "), filtered))
|
||
5 years ago
|
|
||
5 years ago
|
# Capitalise every word in the array and filter out the permissions that are defined within the frozenset
|
||
5 years ago
|
permissions = string.capwords("".join(map(str, DetectPermissions(permission, Perms))))
|
||
5 years ago
|
|
||
|
# Set up the embed to display everything about the user
|
||
|
embed = Embed(
|
||
|
title=f"**User Information**",
|
||
5 years ago
|
colour=Colour(int(random.choice(colour_list))),
|
||
5 years ago
|
timestamp=datetime.datetime.utcnow()
|
||
5 years ago
|
)
|
||
5 years ago
|
embed.set_thumbnail(url=userAvatar)
|
||
|
embed.set_footer(text=f"ID: {target.id}", icon_url='{}'.format(userAvatar))
|
||
|
|
||
|
# Define fields to be added into the embed
|
||
|
embed_fields = [("Name", str(target.mention), True),
|
||
5 years ago
|
("Tag", target.name, True),
|
||
|
("Discrim", "#" + target.discriminator, True),
|
||
5 years ago
|
("Registered", target.created_at.strftime("%a, %b %d, %Y\n%I:%M:%S %p"), True),
|
||
|
("Joined", target.joined_at.strftime("%a, %b %d, %Y\n%I:%M:%S %p"), True),
|
||
5 years ago
|
("Top Role", target.top_role.mention, False),
|
||
5 years ago
|
("Roles", roles, False),
|
||
5 years ago
|
("Key Permissions", permissions, False),
|
||
5 years ago
|
("Status", str(target.status).title(), True),
|
||
|
("Boosting Server", bool(target.premium_since), True),
|
||
|
("Bot", target.bot, True)]
|
||
|
|
||
|
# Add fields to the embed
|
||
|
for name, value, inline in embed_fields:
|
||
|
embed.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
# Send the embed to the channel that the command was triggered in
|
||
|
await ctx.send(embed=embed)
|
||
|
|
||
5 years ago
|
@command(name="serverinfo", aliases=["guildinfo"])
|
||
5 years ago
|
@cooldown(1, 5, BucketType.user)
|
||
5 years ago
|
async def server_info(self, ctx):
|
||
5 years ago
|
"""Allow the users to see information the guild itself"""
|
||
5 years ago
|
|
||
5 years ago
|
# Define guild icon and id
|
||
|
guild_icon = ctx.guild.icon_url
|
||
|
guild_id = ctx.guild.id
|
||
|
|
||
5 years ago
|
# Check if the amount of roles is above 20
|
||
|
if len(ctx.guild.roles) > 20:
|
||
|
# Display the first 20 roles with a length specified telling the user how many roles were not shown
|
||
|
length = len(ctx.guild.roles) - 20
|
||
5 years ago
|
|
||
5 years ago
|
# Store the first 20 roles in a string called "roles"
|
||
|
# (Skipping the first element as it's always going to be @everyone)
|
||
5 years ago
|
role_string = f"{' **>** '.join(map(str, (role.mention for role in ctx.guild.roles[1:20])))} and **{length}** more"
|
||
5 years ago
|
|
||
5 years ago
|
else:
|
||
|
# Display all the roles in the server as it is less than 20
|
||
5 years ago
|
role_string = f"{' **>** '.join(map(str, (role.mention for role in ctx.guild.roles[1:])))}"
|
||
5 years ago
|
|
||
5 years ago
|
# Check if the list of emojis returned are greater than 20
|
||
|
if len(ctx.guild.emojis) > 20:
|
||
5 years ago
|
# Display the first 20 emojis with a length specified telling the user how many emojis were not shown
|
||
5 years ago
|
length = len(ctx.guild.emojis) - 20
|
||
5 years ago
|
# Store the first 20 emojis in a string
|
||
|
emojis = f"{' '.join(map(str, ctx.guild.emojis[:20]))} and **{length}** more..."
|
||
5 years ago
|
else:
|
||
|
# Display all the emojis in the server as it is less than 20
|
||
|
emojis = " ".join(map(str, ctx.guild.emojis))
|
||
5 years ago
|
|
||
5 years ago
|
# Defining a dictionary of the statuses
|
||
|
member_status = {
|
||
|
"online": 0,
|
||
|
"idle": 0,
|
||
|
"dnd": 0,
|
||
|
"offline": 0
|
||
|
}
|
||
|
|
||
|
# Iterating over the members and then storing the numbers in the dictionary
|
||
|
for m in ctx.guild.members:
|
||
|
member_status[str(m.status)] += 1
|
||
|
|
||
|
# Storing the statuses in an array
|
||
|
statuses = [member_status["online"],
|
||
|
member_status["idle"],
|
||
|
member_status["dnd"],
|
||
|
member_status["offline"]]
|
||
5 years ago
|
|
||
5 years ago
|
# Set up embed to display all the server information
|
||
|
embed = Embed(title="**Server Information**",
|
||
5 years ago
|
colour=Colour(int(random.choice(colour_list))),
|
||
5 years ago
|
timestamp=datetime.datetime.utcnow())
|
||
5 years ago
|
embed.set_thumbnail(url=guild_icon)
|
||
|
embed.set_footer(text=f"ID: {guild_id}", icon_url='{}'.format(guild_icon))
|
||
|
|
||
5 years ago
|
# Get the list of banned users from the server
|
||
|
bans = len(await ctx.guild.bans())
|
||
|
# Get the list of invites created for the server
|
||
|
invites = len(await ctx.guild.invites())
|
||
|
|
||
5 years ago
|
# Define fields to be added into the embed
|
||
|
fields = [("Owner", ctx.guild.owner, True),
|
||
5 years ago
|
("Created", ctx.guild.created_at.strftime("%a, %b %d, %Y\n%I:%M:%S %p"), False),
|
||
5 years ago
|
("Region", str(ctx.guild.region).capitalize(), False),
|
||
|
("Statuses", f"🟢 {statuses[0]} \n🟠 {statuses[1]} \n🔴 {statuses[2]} \n⚪ {statuses[3]}", False),
|
||
|
|
||
|
(f"Members ({len(ctx.guild.members)})",
|
||
|
f"\nHumans: {len(list(filter(lambda m: not m.bot, ctx.guild.members)))}" +
|
||
|
f"\nBots: {len(list(filter(lambda m: m.bot, ctx.guild.members)))}" +
|
||
|
f"\nBanned: {bans}", True),
|
||
|
|
||
|
(f"Channels ({len(ctx.guild.channels)})",
|
||
|
f"\nText: {len(ctx.guild.text_channels)}" +
|
||
|
f"\nVoice: {len(ctx.guild.voice_channels)}", True),
|
||
|
|
||
|
("Misc", f"Categories: {len(ctx.guild.categories)}" +
|
||
|
f"\nInvites: {invites}", True),
|
||
|
|
||
|
(f"Roles ({len(ctx.guild.roles)})", role_string, True),
|
||
|
(f"Emojis ({len(ctx.guild.emojis)})", emojis, False)]
|
||
5 years ago
|
|
||
|
# Add fields to the embed
|
||
|
for name, value, inline in fields:
|
||
|
embed.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
# Send the embed to the channel that the command was triggered in
|
||
|
await ctx.send(embed=embed)
|
||
5 years ago
|
|
||
5 years ago
|
@command(name="stats")
|
||
5 years ago
|
async def checking_bot_stats(self, ctx):
|
||
5 years ago
|
"""Bot Statistics (CPU/Mem Usage etc) """
|
||
5 years ago
|
|
||
|
stats = Embed(title="Bot Stats",
|
||
|
colour=enso_embedmod_colours,
|
||
|
timestamp=datetime.datetime.utcnow())
|
||
|
stats.set_thumbnail(url=ctx.guild.icon_url)
|
||
|
|
||
5 years ago
|
# Grabbing technical statistics of the bot
|
||
5 years ago
|
proc = Process()
|
||
|
with proc.oneshot():
|
||
|
uptime = datetime.timedelta(seconds=time() - proc.create_time())
|
||
|
mem_total = virtual_memory().total / (1024 ** 2)
|
||
|
mem_of_total = proc.memory_percent()
|
||
|
mem_usage = mem_total * (mem_of_total / 100)
|
||
|
|
||
5 years ago
|
uptime_hours, uptime_remainder = divmod(uptime.seconds, 3600)
|
||
|
uptime_minutes, uptime_seconds = divmod(uptime_remainder, 60)
|
||
|
frmt_uptime = '{:01} Hours, {:01} Minutes, {:01} Seconds'.format(int(uptime_hours), int(uptime_minutes),
|
||
|
int(uptime_seconds))
|
||
|
|
||
5 years ago
|
# Setting up fields
|
||
5 years ago
|
fields = [
|
||
5 years ago
|
("Developer", "Hamothy#5619", True),
|
||
|
("Bot Version", "1.7.2", False),
|
||
|
("Language | Library", f"Python {python_version()} | Discord.py {discord_version}", False),
|
||
5 years ago
|
("Uptime", frmt_uptime, False),
|
||
5 years ago
|
("Memory Usage", f"{mem_usage:,.2f} / {mem_total:,.2f} MiB ({mem_of_total:.2f}%)", False),
|
||
|
("Guilds", f"{len(self.bot.guilds)}", True),
|
||
|
("Users", f"{len(self.bot.users):,}", True)
|
||
|
]
|
||
|
|
||
|
# Add fields to the embed
|
||
|
for name, value, inline in fields:
|
||
|
stats.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
await ctx.send(embed=stats)
|
||
|
|
||
5 years ago
|
|
||
|
def setup(bot):
|
||
5 years ago
|
bot.add_cog(Info(bot))
|