|
4 | 4 | """ |
5 | 5 |
|
6 | 6 | import re |
| 7 | +import yaml |
| 8 | +import os |
7 | 9 |
|
8 | 10 | import discord |
9 | 11 | from discord.ext import commands |
@@ -214,6 +216,143 @@ async def ping_prefix(self, ctx: commands.Context) -> None: |
214 | 216 | ) |
215 | 217 | await ctx.send(embed=embed) |
216 | 218 |
|
| 219 | + @commands.command(name='createchannel') |
| 220 | + @commands.has_permissions(manage_channels=True) |
| 221 | + async def createchannel(self, ctx: commands.Context) -> None: |
| 222 | + """Create a channel based on properties defined in a YAML file.""" |
| 223 | + yaml_path = "/home/user/Projects/gitcord-template/community/off-topic.yaml" |
| 224 | + |
| 225 | + try: |
| 226 | + # Check if the user has permission to manage channels |
| 227 | + if not ctx.author.guild_permissions.manage_channels: |
| 228 | + embed = create_embed( |
| 229 | + title="❌ Permission Denied", |
| 230 | + description="You need the 'Manage Channels' permission to use this command.", |
| 231 | + color=discord.Color.red() |
| 232 | + ) |
| 233 | + await ctx.send(embed=embed) |
| 234 | + return |
| 235 | + |
| 236 | + # Check if YAML file exists |
| 237 | + if not os.path.exists(yaml_path): |
| 238 | + embed = create_embed( |
| 239 | + title="❌ File Not Found", |
| 240 | + description=f"YAML file not found at: {yaml_path}", |
| 241 | + color=discord.Color.red() |
| 242 | + ) |
| 243 | + await ctx.send(embed=embed) |
| 244 | + return |
| 245 | + |
| 246 | + # Read and parse YAML file |
| 247 | + with open(yaml_path, 'r', encoding='utf-8') as file: |
| 248 | + channel_config = yaml.safe_load(file) |
| 249 | + |
| 250 | + # Validate required fields |
| 251 | + required_fields = ['name', 'type'] |
| 252 | + for field in required_fields: |
| 253 | + if field not in channel_config: |
| 254 | + embed = create_embed( |
| 255 | + title="❌ Invalid YAML", |
| 256 | + description=f"Missing required field: {field}", |
| 257 | + color=discord.Color.red() |
| 258 | + ) |
| 259 | + await ctx.send(embed=embed) |
| 260 | + return |
| 261 | + |
| 262 | + # Prepare channel creation parameters |
| 263 | + channel_kwargs = { |
| 264 | + 'name': channel_config['name'], |
| 265 | + 'topic': channel_config.get('topic', ''), |
| 266 | + 'nsfw': channel_config.get('nsfw', False) |
| 267 | + } |
| 268 | + |
| 269 | + # Set channel type |
| 270 | + if channel_config['type'].lower() == 'text': |
| 271 | + channel_type = discord.ChannelType.text |
| 272 | + elif channel_config['type'].lower() == 'voice': |
| 273 | + channel_type = discord.ChannelType.voice |
| 274 | + else: |
| 275 | + embed = create_embed( |
| 276 | + title="❌ Invalid Channel Type", |
| 277 | + description=f"Channel type '{channel_config['type']}' is not supported. Use 'text' or 'voice'.", |
| 278 | + color=discord.Color.red() |
| 279 | + ) |
| 280 | + await ctx.send(embed=embed) |
| 281 | + return |
| 282 | + |
| 283 | + # Set position if specified |
| 284 | + if 'position' in channel_config: |
| 285 | + channel_kwargs['position'] = channel_config['position'] |
| 286 | + |
| 287 | + # Create the channel based on type |
| 288 | + if channel_type == discord.ChannelType.text: |
| 289 | + new_channel = await ctx.guild.create_text_channel(**channel_kwargs) |
| 290 | + else: # voice channel |
| 291 | + new_channel = await ctx.guild.create_voice_channel(**channel_kwargs) |
| 292 | + |
| 293 | + # Create success embed |
| 294 | + embed = create_embed( |
| 295 | + title="✅ Channel Created", |
| 296 | + description=f"Successfully created channel: {new_channel.mention}", |
| 297 | + color=discord.Color.green() |
| 298 | + ) |
| 299 | + |
| 300 | + # Add fields manually |
| 301 | + embed.add_field(name="Name", value=channel_config['name'], inline=True) |
| 302 | + embed.add_field(name="Type", value=channel_config['type'], inline=True) |
| 303 | + embed.add_field(name="NSFW", value=str(channel_config.get('nsfw', False)), inline=True) |
| 304 | + embed.add_field(name="Topic", value=channel_config.get('topic', 'No topic set'), inline=False) |
| 305 | + |
| 306 | + await ctx.send(embed=embed) |
| 307 | + logger.info("Channel '%s' created successfully by %s", channel_config['name'], ctx.author) |
| 308 | + |
| 309 | + except yaml.YAMLError as e: |
| 310 | + embed = create_embed( |
| 311 | + title="❌ YAML Parse Error", |
| 312 | + description=f"Failed to parse YAML file: {str(e)}", |
| 313 | + color=discord.Color.red() |
| 314 | + ) |
| 315 | + await ctx.send(embed=embed) |
| 316 | + logger.error("YAML parse error in createchannel command: %s", e) |
| 317 | + except discord.Forbidden: |
| 318 | + embed = create_embed( |
| 319 | + title="❌ Permission Error", |
| 320 | + description="I don't have permission to create channels in this server.", |
| 321 | + color=discord.Color.red() |
| 322 | + ) |
| 323 | + await ctx.send(embed=embed) |
| 324 | + logger.error("Permission error in createchannel command") |
| 325 | + except discord.HTTPException as e: |
| 326 | + embed = create_embed( |
| 327 | + title="❌ Discord Error", |
| 328 | + description=f"Discord API error: {str(e)}", |
| 329 | + color=discord.Color.red() |
| 330 | + ) |
| 331 | + await ctx.send(embed=embed) |
| 332 | + logger.error("Discord HTTP error in createchannel command: %s", e) |
| 333 | + except Exception as e: # pylint: disable=broad-except |
| 334 | + embed = create_embed( |
| 335 | + title="❌ Unexpected Error", |
| 336 | + description=f"An unexpected error occurred: {str(e)}", |
| 337 | + color=discord.Color.red() |
| 338 | + ) |
| 339 | + await ctx.send(embed=embed) |
| 340 | + logger.error("Unexpected error in createchannel command: %s", e) |
| 341 | + |
| 342 | + @createchannel.error |
| 343 | + async def createchannel_error(self, ctx: commands.Context, error: commands.CommandError) -> None: |
| 344 | + """Handle errors for the createchannel command.""" |
| 345 | + if isinstance(error, commands.MissingPermissions): |
| 346 | + embed = create_embed( |
| 347 | + title="❌ Permission Denied", |
| 348 | + description="You need the 'Manage Channels' permission to use this command.", |
| 349 | + color=discord.Color.red() |
| 350 | + ) |
| 351 | + await ctx.send(embed=embed) |
| 352 | + else: |
| 353 | + logger.error("Error in createchannel command: %s", error) |
| 354 | + await ctx.send(f"An error occurred: {error}") |
| 355 | + |
217 | 356 | @commands.Cog.listener() |
218 | 357 | async def on_command_error(self, ctx: commands.Context, error: commands.CommandError) -> None: |
219 | 358 | """Handle command errors.""" |
|
0 commit comments