admin.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import discord
  2. from discord import app_commands
  3. from discord.ext import commands
  4. from discord.ui import ChannelSelect
  5. #from common.logging import report
  6. from common.settings import check_ignore
  7. from query.guild import set_output_channel
  8. class View(discord.ui.View):
  9. def __init__(self, pg):
  10. super().__init__()
  11. self.pg = pg
  12. @discord.ui.select(cls=ChannelSelect, channel_types=[discord.ChannelType.text])
  13. async def select_channels(self, interaction: discord.Interaction, select: ChannelSelect):
  14. await set_output_channel(self.pg, interaction.guild_id, select.values[0].id)
  15. return await interaction.response.send_message(f'You selected {select.values[0].mention}')
  16. class Select(discord.ui.Select):
  17. def __init__(self, pg, channels):
  18. self.pg = pg
  19. print(channels)
  20. options = []
  21. for channel in channels:
  22. options.append(discord.SelectOption(label=str(channel.name),emoji="👌",description=str(channel.category)),)
  23. print(options)
  24. #options=[
  25. # discord.SelectOption(label="Option 1",emoji="👌",description="This is option 1!"),
  26. # discord.SelectOption(label="Option 2",emoji="✨",description="This is option 2!"),
  27. # discord.SelectOption(label="Option 3",emoji="🎭",description="This is option 3!")
  28. # ]
  29. super().__init__(placeholder="Select an option",max_values=1,min_values=1,options=options)
  30. async def callback(self, interaction: discord.Interaction):
  31. await set_output_channel(self.pg, interaction.guild_id, 971796738484625468)
  32. await interaction.response.send_message(content=f"Your choice is {self.values[0]}!", ephemeral=True)
  33. class SelectView(discord.ui.View):
  34. def __init__(self, pg, channels, *, timeout = 180):
  35. super().__init__(timeout=timeout)
  36. self.add_item(Select(pg, channels))
  37. class Admin(commands.Cog):
  38. """Administrative functionality."""
  39. def __init__(self, bot: commands.Bot):
  40. self.bot = bot
  41. @app_commands.command(
  42. name="guildsettings",
  43. description="Modify guild specific settings",
  44. )
  45. @commands.has_guild_permissions(administrator=True)
  46. async def guildsettings(self, interaction: discord.Interaction):
  47. # Halt on ignore list.
  48. if await check_ignore(self.bot.pg, interaction.user):
  49. return
  50. #await ctx.send("Menus!", view=SelectView(self.bot.pg, ctx.guild.channels))
  51. await interaction.response.send_message("Menus!", view=View(self.bot.pg), ephemeral=True)
  52. @commands.command(
  53. name="sync",
  54. description="Sync commands",
  55. brief="Synchronise commands with Discord",
  56. help="Update the bot commands"
  57. )
  58. @commands.is_owner()
  59. async def sync(self, ctx: commands.Context) -> None:
  60. synced = await ctx.bot.tree.sync()
  61. await ctx.send(f"Synced {len(synced)} commands globally")
  62. @commands.command(
  63. name="testsync",
  64. description="Sync commands",
  65. brief="Synchronise commands to this Discord",
  66. help="Update the bot commands"
  67. )
  68. @commands.is_owner()
  69. async def testsync(self, ctx: commands.Context) -> None:
  70. ctx.bot.tree.copy_global_to(guild=ctx.guild)
  71. synced = await ctx.bot.tree.sync(guild=ctx.guild)
  72. await ctx.send(content=f"Synced {len(synced)} commands to this guild", ephemeral=True)
  73. async def setup(bot: commands.Bot):
  74. await bot.add_cog(Admin(bot))