Skip to content

Commit

Permalink
/ command to create voice create channel
Browse files Browse the repository at this point in the history
  • Loading branch information
camalot committed May 11, 2024
1 parent 30fc8ab commit eb364ec
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 93 deletions.
48 changes: 48 additions & 0 deletions bot/cogs/lib/mongodb/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,54 @@ def __init__(self) -> None:
)
pass

def get_create_channel(self, guildId: int, voiceChannelId: int):
_method = inspect.stack()[0][3]
try:
if self.connection is None:
self.open()
result = self.connection.create_channels.find_one(
{"guild_id": str(guildId), "voice_channel_id": str(voiceChannelId)}
)
return result
except Exception as ex:
self.log(
guildId=guildId,
level=LogLevel.ERROR,
method=f"{self._module}.{self._class}.{_method}",
message=f"{ex}",
stackTrace=traceback.format_exc(),
)
return None

def set_create_channel(self, guildId: int, voiceChannelId: int, categoryId: int, ownerId: int, useStage: bool):
_method = inspect.stack()[0][3]
try:
if self.connection is None:
self.open()
payload = {
"guild_id": str(guildId),
"voice_channel_id": str(voiceChannelId),
"voice_category_id": str(categoryId),
"owner_id": str(ownerId),
"use_stage": useStage,
"timestamp": utils.get_timestamp(),
}
self.connection.create_channels.update_one(
{"guild_id": str(guildId), "voice_channel_id": str(voiceChannelId)},
{"$set": payload},
upsert=True,
)
return True
except Exception as ex:
self.log(
guildId=guildId,
level=LogLevel.ERROR,
method=f"{self._module}.{self._class}.{_method}",
message=f"Failed to set create channel: {ex}",
stackTrace=traceback.format_exc(),
)
return False

def track_channel_name(self, guildId: int, channelId: int, ownerId: int, name: str) -> None:
_method = inspect.stack()[0][3]
try:
Expand Down
164 changes: 71 additions & 93 deletions bot/cogs/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from discord import app_commands
from discord.ext import commands
import traceback
import json

from time import gmtime, strftime
import os
Expand Down Expand Up @@ -471,110 +472,87 @@ async def channel(self, ctx) -> None:
@app_commands.default_permissions(administrator=True)
@group.command(name="channel", description="Create a new CREATE voice channel")
@app_commands.describe(channel_name="The name of the channel to create. Default: CREATE CHANNEL 🔊")
@app_commands.describe(category="The category to create the channel in and all dynamic channels. Default: None")
@app_commands.describe(category="The category to create the channel in and all dynamic channels.")
async def channel_app_command(
self,
interaction: discord.Interaction,
channel_name: typing.Optional[str] = None,
category: typing.Optional[discord.CategoryChannel] = None
category: discord.CategoryChannel,
channel_name: typing.Optional[str] = "CREATE CHANNEL 🔊",
use_stage: typing.Optional[bool] = False,
):
pass

@setup.command(name='init', aliases=['i'])
@commands.guild_only()
@commands.has_permissions(administrator=True)
async def init(self, ctx):
_method = inspect.stack()[0][3]
guild_id = ctx.guild.id
try:
# this should be configurable
exclude_roles = [r.id for r in ctx.guild.roles if r.name.lower().startswith('lfg-')]
async def rsv_callback(view, interaction):
await interaction.response.defer()
await rsv_message.delete()
# get the role
role_id = int(interaction.data['values'][0])
# get the role object
role = utils.get_by_name_or_id(ctx.guild.roles, role_id)
if role is None:
await self.messaging.send_embed(
ctx.channel,
self.settings.get_string(guild_id, "title_role_not_found"),
f"{ctx.author.mention}, {self.settings.get_string(guild_id, 'info_role_not_found')}",
delete_after=5,
)
return
if interaction.guild is None:
return

await ctx.send(f"Role selected: {role.name}")
selected_role = role

async def csv_callback(view, interaction):
await interaction.response.defer()
await csv_message.delete()

category_id = int(interaction.data['values'][0])
if category_id == -2:
# create a new category
await ctx.send("TODO: Create a new category", delete_after=5)
return
if category_id == -1:
# no category selected
await ctx.send("TODO: No category selected", delete_after=5)
return
if category_id == 0:
# other category selected
await ctx.send("TODO: Other category selected", delete_after=5)
return

# get the role object
category: discord.CategoryChannel = utils.get_by_name_or_id(ctx.guild.categories, category_id)
if not category:
await ctx.send("Category not found", delete_after=5)
selected_category = category
await ctx.send(f"Category selected: {category.name}")


async def csv_timeout_callback(view):
await csv_message.delete()
# took too long to respond
await self.messaging.send_embed(ctx.channel, self.settings.get_string(guild_id, "title_timeout"), f"{ctx.author.mention}, {self.settings.get_string(guild_id, 'took_too_long')}", delete_after=5)
pass


csv = CategorySelectView(
ctx=ctx,
placeholder=self.settings.get_string(guild_id, "title_select_category"),
categories=ctx.guild.categories,
select_callback=csv_callback,
timeout_callback=csv_timeout_callback,
allow_none=False,
allow_new=True,
timeout=60
)
csv_message = await ctx.send(view=csv)
async def rsv_timeout_callback(view):
await rsv_message.delete()
# took too long to respond
await self.messaging.send_embed(ctx.channel, self.settings.get_string(guild_id, "title_timeout"), f"{ctx.author.mention}, {self.settings.get_string(guild_id, 'took_too_long')}", delete_after=5)
pass
# is user an admin
if not self._users.isAdmin(interaction):
await interaction.response.send_message("You are not an administrator of the bot. You cannot use this command.", ephemeral=True)
return

guild_id = interaction.guild.id
try:
if channel_name is None or channel_name == "":
channel_name = "CREATE CHANNEL 🔊"
if use_stage is None:
use_stage = False

self.log.debug(guild_id, _method, f"category: {category}")

selected_role = None
selected_category = None
# ask for the default user role.
rsv = RoleSelectView(
ctx=ctx,
placeholder=self.settings.get_string(guild_id, "title_select_default_role"),
exclude_roles=exclude_roles,
select_callback=rsv_callback,
timeout_callback=rsv_timeout_callback,
allow_none=False,
default_role = self.settings.db.get_default_role(
guildId=guild_id, categoryId=category.id, userId=None
)
self.log.debug(guild_id, _method, f"default_role: {default_role}")
temp_default_role = utils.get_by_name_or_id(interaction.guild.roles, default_role)
self.log.debug(guild_id, _method, f"temp_default_role: {temp_default_role}")
system_default_role = interaction.guild.default_role
temp_default_role = temp_default_role if temp_default_role else system_default_role
if use_stage:
new_channel = await category.create_stage_channel(
name=channel_name,
bitrate=64 * 1000,
user_limit=0,
position=0,
# overrites={
# temp_default_role: discord.PermissionOverwrite(
# view_channel=True,
# connect=True,
# speak=True,
# stream=False,
# use_voice_activation=True
# )
# },
reason=f"Created for VoiceCreateBot by {interaction.user.name}"
)
else:
new_channel = await category.create_voice_channel(
name=channel_name,
bitrate=64 * 1000,
user_limit=0,
position=0,
# overrites={
# temp_default_role: discord.PermissionOverwrite(
# view_channel=True,
# connect=True,
# speak=True,
# stream=False,
# use_voice_activation=True
# )
# },
reason=f"Created for VoiceCreateBot by {interaction.user.name}"
)

timeout=60
self.channel_db.set_create_channel(
guildId=guild_id,
voiceChannelId=new_channel.id,
categoryId=category.id,
ownerId=interaction.user.id,
useStage=use_stage
)
rsv_message = await ctx.send(view=rsv)

await interaction.response.send_message(f"Channel {new_channel.mention} created", ephemeral=True)
except Exception as e:
self.log.error(ctx.guild.id, f"{self._module}.{_method}", f"{e}", traceback.format_exc())
self.log.error(guild_id, _method, str(e), traceback.format_exc())
pass

@setup.group(name="role", aliases=['r'])
@commands.guild_only()
Expand Down

0 comments on commit eb364ec

Please sign in to comment.