admin.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. from discord.ext import commands
  2. import discord
  3. import time
  4. from typing import Optional
  5. from query.channel import get_interact
  6. from query.user import ignore_user, unignore_user, is_ignored
  7. from common.logging import report
  8. from common.settings import check_ignore
  9. class ChannelSettingsModal(discord.ui.Modal):
  10. def __init__(self, *args, **kwargs) -> None:
  11. super().__init__(*args, **kwargs)
  12. self.add_item(discord.ui.InputText(label="Interact"))
  13. self.add_item(discord.ui.InputText(label="Games", style=discord.InputTextStyle.long))
  14. async def callback(self, interaction: discord.Interaction):
  15. print(f"interaction: {interaction}")
  16. embed = discord.Embed(title="Test Modal Results")
  17. embed.add_field(name="Interact", value=self.children[0].value)
  18. embed.add_field(name="Games", value=self.children[1].value)
  19. await interaction.response.send_message(embeds=[embed])
  20. class Admin(commands.Cog):
  21. def __init__(self, bot): # Special method that is called when the cog is loaded
  22. self.bot = bot
  23. @commands.slash_command()
  24. async def modal_slash(self, ctx: discord.ApplicationContext):
  25. """Shows an example of a modal dialog being invoked from a slash command."""
  26. modal = ChannelSettingsModal(title="Adjust channel functions.")
  27. await ctx.send_modal(modal)
  28. @commands.slash_command(
  29. description="Get the bot's current websocket and API latency.",
  30. brief="Test latency",
  31. help="Test latency by polling the gateway and API."
  32. )
  33. @commands.has_permissions(administrator=True)
  34. async def ping(self, ctx: commands.Context):
  35. # Halt on ignore list.
  36. if await check_ignore(self.bot.pg, ctx.author):
  37. return
  38. start_time = time.time()
  39. await ctx.respond(f"Pong!\nGateway heartbeat in {round(self.bot.latency * 1000)}ms.")
  40. end_time = time.time()
  41. await ctx.send(f"API roundtrip latency {round((end_time - start_time) * 1000)}ms.")
  42. @commands.slash_command(
  43. description="Send a message",
  44. brief="Chat",
  45. help="Make a chat message",
  46. aliases= ("say", "pm", "dm", "echo", "print")
  47. )
  48. #async def msg(self, ctx: commands.Context, channel: Optional[discord.TextChannel], user: Optional[discord.User], *, message: str = None):
  49. async def msg(self, ctx: commands.Context, channel: Optional[discord.TextChannel], user: Optional[discord.User], message: str = None):
  50. # Halt on ignore list.
  51. if await check_ignore(self.bot.pg, ctx.author):
  52. return
  53. #print(f"Is Guild Admin: {ctx.author.guild_permissions.administrator}")
  54. #print(f"Author has perms in main output channel: {self.bot.get_channel(self.bot.OUTPUT_CHANNEL_ID).permissions_for(ctx.author).send_messages}")
  55. #print(f"Has author perms in main output channel: {ctx.author.permissions_in(self.bot.get_channel(self.bot.OUTPUT_CHANNEL_ID)).send_messages}")
  56. if not message:
  57. if channel:
  58. await ctx.respond(f"What would you like me to say in `{channel}`?")
  59. elif user:
  60. await ctx.respond(f"What would you like me to say to `{user}`?")
  61. else:
  62. await ctx.respond("What would you like me to say?")
  63. elif channel:
  64. if await get_interact(self.bot.pg, channel.id) or channel.permissions_for(ctx.author).administrator or self.bot.get_channel(self.bot.OUTPUT_CHANNEL_ID).permissions_for(ctx.author).send_messages:
  65. await channel.send(message)
  66. await ctx.respond("Message sent.")
  67. await report(self.bot, f"`{ctx.author}` @ {channel.mention}: {message}", ctx.guild)
  68. else:
  69. await ctx.respond(f"Interactive mode for {channel} is deactivated.")
  70. elif user and self.bot.get_channel(self.bot.OUTPUT_CHANNEL_ID).permissions_for(ctx.author).send_messages:
  71. await user.send(message)
  72. await ctx.respond("Message sent.")
  73. await report(self.bot, f"`{ctx.author}` @ `{user.name}`: {message}")
  74. else:
  75. await ctx.respond(message)
  76. await report(self.bot, f"`{ctx.author}` has sent {message} locally.", ctx.guild)
  77. @commands.slash_command(
  78. description="Change status.",
  79. brief="Set status",
  80. help="Update the bot's status."
  81. )
  82. async def status(self, ctx: commands.Context, *, text: str):
  83. # Halt on ignore list.
  84. if await check_ignore(self.bot.pg, ctx.author):
  85. return
  86. await self.bot.change_presence(activity=discord.Game(name=text))
  87. await report(self.bot, f"`{ctx.author}` has set my status to `{text}`.")
  88. @commands.slash_command(
  89. description="Get ignored.",
  90. brief="Ignore sender",
  91. help="Will have the bot ignore the user from now on."
  92. )
  93. async def ignoreme(self, ctx: commands.Context):
  94. # Halt on ignore list.
  95. if await check_ignore(self.bot.pg, ctx.author):
  96. return
  97. await ignore_user(self.bot.pg, ctx.author.id)
  98. await ctx.respond("To revert this use the `/unignoreme` command.")
  99. await report(self.bot, f"`{ctx.author}` has requested to be ignored.")
  100. @commands.slash_command(
  101. description="No longer get ingored.",
  102. brief="Un-ignore sender",
  103. help="No longer will the bot ignore the user."
  104. )
  105. async def unignoreme(self, ctx: commands.Context):
  106. await unignore_user(self.bot.pg, ctx.author.id)
  107. await ctx.respond(f"I shall now interact with you again where my channel settings allow it.")
  108. await report(self.bot, f"`{ctx.author}` has requested to be un-ignored.")
  109. @commands.slash_command(
  110. description="Ignore status for user.",
  111. brief="Check if user is ingored",
  112. help="Verify if the user is being ignored."
  113. )
  114. async def isignored(self, ctx: commands.Context, user: Optional[discord.User]):
  115. # Halt on ignore list.
  116. if await check_ignore(self.bot.pg, ctx.author):
  117. return
  118. if not user:
  119. user = ctx.author
  120. if await is_ignored(self.bot.pg, user.id):
  121. await ctx.respond(f"I am ingoring `{user}`.")
  122. else:
  123. await ctx.respond(f"I am not ignoring `{user}`.")
  124. def setup(bot): # Called by Pycord to setup the cog
  125. bot.add_cog(Admin(bot)) # Add the cog to the bot