admin.py 7.0 KB

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