admin.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 query.guild import set_output_channel
  7. class SelectOutputChannel(discord.ui.View):
  8. def __init__(self, pg):
  9. super().__init__()
  10. self.pg = pg
  11. @discord.ui.select(cls=ChannelSelect, channel_types=[discord.ChannelType.text])
  12. async def select_channels(self, interaction: discord.Interaction, select: ChannelSelect):
  13. await set_output_channel(self.pg, interaction.guild_id, select.values[0].id)
  14. return await interaction.response.send_message(f'You selected {select.values[0].mention}')
  15. # class SelectOutputChannel(discord.ui.Select):
  16. # def __init__(self, pg, channels):
  17. # self.pg = pg
  18. # options = []
  19. # for channel in channels:
  20. # options.append(discord.SelectOption(label=str(channel.name),emoji="👌",description=str(channel.category)),)
  21. # super().__init__(placeholder="Select an option",max_values=1,min_values=1,options=options)
  22. # async def callback(self, interaction: discord.Interaction):
  23. # await set_output_channel(self.pg, interaction.guild_id, 971796738484625468)
  24. # await interaction.response.send_message(content=f"Your choice is {self.values[0]}!", ephemeral=True)
  25. # class SelectInteract(discord.ui.Select):
  26. # def __init__(self, pg):
  27. # self.pg = pg
  28. # options = [
  29. # discord.SelectOption(label="Option 1", emoji=":x:", description="Disabled"),
  30. # discord.SelectOption(label="Option 2", emoji=":white_square_button:", description="Defer to default"),
  31. # discord.SelectOption(label="Option 3", emoji=":white_check_mark:", description="Enabled")
  32. # ]
  33. # super().__init__(placeholder="Select an option", max_values=1, min_values=1, options=options)
  34. # async def callback(self, interaction: discord.Interaction):
  35. # await interaction.response.send_message(content=f"Your choice is {self.values[0]}!", ephemeral=True)
  36. #
  37. # class SelectGames(discord.ui.Select):
  38. # def __init__(self, pg, channels):
  39. # self.pg = pg
  40. # options = [
  41. # discord.SelectOption(label="Option 1", emoji=":x:", description="Disabled"),
  42. # discord.SelectOption(label="Option 2", emoji=":white_square_button:", description="Defer to default"),
  43. # discord.SelectOption(label="Option 3", emoji=":white_check_mark:", description="Enabled")
  44. # ]
  45. # super().__init__(placeholder="Select an option", max_values=1, min_values=1, options=options)
  46. # async def callback(self, interaction: discord.Interaction):
  47. # await interaction.response.send_message(content=f"Your choice is {self.values[0]}!", ephemeral=True)
  48. # class SelectView(discord.ui.View):
  49. # def __init__(self, pg, channels, *, timeout = 180):
  50. # super().__init__(timeout=timeout)
  51. # self.add_item(SelectOutputChannel(pg, channels))
  52. class Admin(commands.Cog):
  53. """Administrative functionality."""
  54. def __init__(self, bot: commands.Bot):
  55. self.bot = bot
  56. @app_commands.command(
  57. name="guildsettings",
  58. description="Modify guild specific settings",
  59. )
  60. @commands.has_guild_permissions(administrator=True)
  61. async def guildsettings(self, interaction: discord.Interaction):
  62. #await ctx.send("Menus!", view=SelectView(self.bot.pg, ctx.guild.channels))
  63. await interaction.response.send_message("Menus!", view=SelectOutputChannel(self.bot.pg), ephemeral=True)
  64. @commands.command(
  65. name="sync",
  66. description="Sync commands",
  67. brief="Synchronise commands with Discord",
  68. help="Update the bot commands"
  69. )
  70. @commands.is_owner()
  71. async def sync(self, ctx: commands.Context) -> None:
  72. synced = await ctx.bot.tree.sync()
  73. await ctx.send(f"Synced {len(synced)} commands globally")
  74. @commands.command(
  75. name="testsync",
  76. description="Sync commands",
  77. brief="Synchronise commands to this Discord",
  78. help="Update the bot commands"
  79. )
  80. @commands.is_owner()
  81. async def testsync(self, ctx: commands.Context) -> None:
  82. ctx.bot.tree.copy_global_to(guild=ctx.guild)
  83. synced = await ctx.bot.tree.sync(guild=ctx.guild)
  84. await ctx.send(content=f"Synced {len(synced)} commands to this guild", ephemeral=True)
  85. @app_commands.command(
  86. name="status",
  87. description="Change status.",
  88. )
  89. async def status(self, interaction: discord.Interaction, message: str) -> None:
  90. await self.bot.change_presence(activity=discord.Game(name=message))
  91. await interaction.response.send_message(content=":white_check_mark:")
  92. await report(self.bot, f" has set my status to `{message}`.", user=interaction.user)
  93. async def setup(bot: commands.Bot):
  94. await bot.add_cog(Admin(bot))