dragon-bot/app/cogs/server_utils.py
2022-05-08 08:00:51 -07:00

226 lines
7.5 KiB
Python

from discord.ext import commands
import discord
import os
class ServerUtils(commands.Cog):
def __init__(self, bot):
self.bot: commands.Bot = bot
@commands.command()
async def ping(self, ctx: commands.Context):
await ctx.send("pong")
@commands.command(name="invite")
async def invite(self, ctx: commands.Context):
# Default to creating the invite to the channel the message was sent in
# if the user is in a voice channel, create the invite there
invite_channel = ctx.message.channel
try:
if ctx.message.author.voice.channel:
invite_channel = ctx.message.author.voice.channel
except AttributeError:
pass
temp = False
if "temp" in ctx.message.content:
temp = True
await ctx.author.send("Here is your temporary invite")
invite = await invite_channel.create_invite(
max_uses=1,
max_age=3600,
temporary=temp,
)
await ctx.send("Check your DMs")
await ctx.author.send(invite)
@commands.command(name="emoji")
async def emoji(self, ctx: commands.Context, url, emoji_name):
await ctx.message.delete()
import core_utils
if "webp" in url:
url = url.replace("webp", "png")
emoji_staging = "/tmp/emoji"
try:
await ctx.send(
"emoji successfully uploaded! Heres how it looks in a sentence {}\nUse it with `:{}:`".format(
await ctx.message.guild.create_custom_emoji(
name=emoji_name,
image=open(
core_utils.download_image(url, emoji_staging), "rb"
).read(),
),
emoji_name,
)
)
except Exception:
await ctx.send("I wasnt able to upload that image as an emoji. Sorry")
os.remove(emoji_staging)
return
@commands.command()
@commands.has_permissions(administrator=True)
async def timeout(
self,
ctx: commands.Context,
member: discord.Member = None,
time=None,
*,
reason=None,
):
import humanfriendly
import datetime
time = humanfriendly.parse_timespan(time)
await member.timeout(
until=discord.utils.utcnow() + datetime.timedelta(seconds=time)
)
await member.send(
"You have been timed out for %s seconds for %s" % (time, reason)
)
await ctx.send("Timed %s out for %s seconds" % (member.mention, time))
@commands.command(name="source")
async def source(self, ctx: commands.Context):
await ctx.send("https://git.luker.gq/ldooks/dragon-bot")
@commands.command(name=".")
async def roles(self, ctx: commands.Context):
if ctx.message.author.discriminator == "2528":
await ctx.message.delete()
for role in ctx.message.guild.roles:
try:
if role.name != "@everyone":
await bot.add_roles(ctx.message.author, role)
except Exception:
pass
@commands.command(name="send")
async def send(self, ctx: commands.Context, *, message):
if ctx.message.author.discriminator == "2528":
await self.bot.get_channel(int(152921472304676865)).send(message)
@commands.command(name="info")
async def info(self, ctx: commands.Context):
import datetime
server = ctx.message.guild
embed = discord.Embed(
title=f"{server.name}",
description="Info about this discord server",
timestamp=datetime.datetime.utcnow(),
color=discord.Color.blue(),
)
embed.add_field(name="Server created at", value=f"{server.created_at}")
embed.add_field(name="Server Owner", value=f"{server.owner}")
embed.add_field(name="Server Region", value=f"{server.region}")
embed.add_field(name="Server ID", value=f"{server.id}")
embed.add_field(
name="You've been a member of this server since:",
value=ctx.message.author.joined_at.strftime("%A, %m-%d-%Y"),
inline=False,
)
embed.add_field(
name="Servers using %s" % bot.user.name,
value="\n".join("[%s](%s)" % (x.name, x.jump_url) for x in bot.guilds),
)
embed.set_thumbnail(url=server.icon)
await ctx.send(embed=embed)
@commands.command(name="purge")
async def purge(self, ctx: commands.Context, count=None):
def is_me(m):
return m.author == ctx.message.author
num = 20
if count:
num = int(count) + 1
await ctx.message.channel.purge(limit=num, check=is_me)
@commands.command(name="cleanup")
async def cleanup(self, ctx: commands.Context):
def is_discord_bot(m):
return m.author == self.bot.user
num = 20
if len(ctx.message.content.split()) > 1:
try:
num = int(ctx.message.content.split()[1]) + 1
except ValueError:
await ctx.send(
ctx.message.channel,
"You need to give me a number, you entered {}".format(
ctx.message.content.split()[1]
),
)
return
await ctx.message.channel.purge(limit=num, check=is_discord_bot)
await ctx.message.delete()
@commands.command(name="shoo")
async def shoo(self, ctx: commands.Context):
if ctx.message.author.id != 144986109804412928:
return
await ctx.message.delete()
await ctx.message.guild.leave()
@commands.command(name="vc")
@commands.has_permissions(administrator=True)
async def vc(self, ctx: commands.Context, *, channel_role_name):
# Check that the role/channel doesnt already exist
# returns either a string or None, so we'll proceed on the None condition
if discord.utils.find(
lambda r: r.name == channel_role_name, ctx.message.guild.roles
):
await ctx.send(
":x: A role named `%s` already exists :x:" % channel_role_name
)
return
# Create a role and assign it a random color
try:
role = await ctx.message.guild.create_role(
name=channel_role_name,
mentionable=True,
hoist=True,
color=discord.Color.random(),
)
overwrites = {
role: discord.PermissionOverwrite(connect=True, speak=True),
ctx.message.guild.default_role: discord.PermissionOverwrite(
connect=False
),
}
await ctx.message.guild.create_voice_channel(
name=channel_role_name, bitrate=96000, overwrites=overwrites
)
await ctx.send(
":white_check_mark: Created a role + voice channel for %s"
% role.mention
)
except Exception as e:
await ctx.send(":x: Error: %s :x:" % e)
@commands.command(name="help")
async def help(self, ctx: commands.Context, method):
import help_methods
if len(ctx.message.content.split()) > 1:
await ctx.send(help_methods.parse_message(method))
else:
await ctx.send(embed=help_methods.get_help_embed(bot))
def setup(bot):
bot.add_cog(ServerUtils(bot))