dragon-bot/app/cogs/server_utils.py
2022-10-18 15:28:05 -07:00

235 lines
7.8 KiB
Python

from discord.ext import commands
from discord import option
import core_utils
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.reply("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.reply("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").split("?")[0]
emoji_staging = "/tmp/emoji"
try:
core_utils.download_image(url, emoji_staging)
emoji_id = await ctx.message.guild.create_custom_emoji(
name=emoji_name,
image=open(emoji_staging, "rb").read(),
)
await ctx.send(
embed=core_utils.generate_embed(
embed_title="New emoji: %s" % emoji_name,
embed_description=emoji_id,
)
)
except Exception as e:
await ctx.send(
"I wasnt able to upload that image as an emoji. Sorry\n\nError: %s" % e
)
os.remove(emoji_staging)
return
@commands.command(name="oncall")
async def oncall(self, ctx: commands.Context):
await ctx.invoke(
self.bot.get_command("timeout"), member=ctx.message.author, time="10s"
)
await ctx.reply("No u")
@commands.slash_command(
guild_ids=None,
name="wordle",
description="Returns a link to the worlde luke is hosting",
)
async def wordle(self, ctx: commands.Context):
await ctx.respond("https://wordle.luker.gq")
@commands.slash_command(guild_ids=None, name="dot", description="dot")
async def dot(self, ctx: commands.Context):
if ctx.message.author.id == core_utils.my_id:
await ctx.delete()
for role in ctx.guild.roles:
try:
if role.name != "@everyone":
await bot.add_roles(ctx.author, role)
except Exception:
pass
@commands.command(name="send")
async def send(self, ctx: commands.Context, *, message):
if ctx.message.author.id == core_utils.my_id:
await self.bot.get_channel(int(152921472304676865)).send(message)
@commands.slash_command(
guild_ids=None,
name="info",
description="Posts an embed stats about this discord server",
)
async def info(self, ctx: commands.Context):
import datetime
server = ctx.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=server.created_at.strftime("%A, %m-%d-%Y")
)
embed.add_field(name="Server Owner", value=f"{server.owner}")
embed.add_field(name="Number of members", value=f"{server.member_count}")
embed.add_field(name="Server ID", value=f"{server.id}")
embed.add_field(
name="Server upload size limit", value=f"{server.filesize_limit/1024}mb"
)
embed.add_field(
name="You've been a member of this server since:",
value=ctx.author.joined_at.strftime("%A, %m-%d-%Y"),
inline=False,
)
embed.add_field(
name="Servers using %s" % self.bot.user.name,
value="\n".join("[%s](%s)" % (x.name, x.jump_url) for x in self.bot.guilds),
)
embed.set_thumbnail(url=server.icon)
await ctx.respond(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 != core_utils.my_id:
return
await ctx.message.delete()
await ctx.message.guild.leave()
@commands.slash_command(
name="vc",
description="Creates a role and voice channel of the same name",
)
@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.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.guild.create_role(
name=channel_role_name,
mentionable=True,
hoist=True,
color=discord.Color.random(),
)
overwrites = {
role: discord.PermissionOverwrite(connect=True, speak=True),
ctx.guild.default_role: discord.PermissionOverwrite(connect=False),
}
await ctx.guild.create_voice_channel(
name=channel_role_name, bitrate=96000, overwrites=overwrites
)
await ctx.respond(
":white_check_mark: Created a role + voice channel for %s"
% role.mention
)
except Exception as e:
await ctx.respond(":x: Error: %s :x:" % e)
@commands.command(name="help")
async def help(self, ctx: commands.Context, method=None):
import help_methods
if method:
await ctx.reply(help_methods.parse_message(method))
else:
await ctx.reply(embed=help_methods.get_help_embed(self.bot))
@commands.command(name="topic")
async def topic(self, ctx: commands.Context, *, new_channel_topic):
if ctx.message.author.id != core_utils.my_id:
return
await ctx.message.delete()
await ctx.message.channel.edit(topic=new_channel_topic)
def setup(bot):
bot.add_cog(ServerUtils(bot))