1
0

admin.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 Admin(commands.Cog):
  10. def __init__(self, bot): # Special method that is called when the cog is loaded
  11. self.bot = bot
  12. @commands.slash_command(
  13. description="Get the bot's current websocket and API latency.",
  14. brief="Test latency",
  15. help="Test latency by polling the gateway and API."
  16. )
  17. @commands.has_permissions(administrator=True)
  18. async def ping(self, ctx: commands.Context):
  19. # Halt on ignore list.
  20. if await check_ignore(self.bot.pg, ctx.author):
  21. return
  22. start_time = time.time()
  23. await ctx.respond(f"Pong!\nGateway heartbeat in {round(self.bot.latency * 1000)}ms.")
  24. end_time = time.time()
  25. await ctx.send(f"API roundtrip latency {round((end_time - start_time) * 1000)}ms.")
  26. @commands.slash_command(
  27. description="Send a message",
  28. brief="Chat",
  29. help="Make a chat message",
  30. aliases= ("say", "pm", "dm", "echo", "print")
  31. )
  32. #async def msg(self, ctx: commands.Context, channel: Optional[discord.TextChannel], user: Optional[discord.User], *, message: str = None):
  33. async def msg(self, ctx: commands.Context, channel: Optional[discord.TextChannel], user: Optional[discord.User], message: str = None):
  34. # Halt on ignore list.
  35. if await check_ignore(self.bot.pg, ctx.author):
  36. return
  37. #print(f"Is Guild Admin: {ctx.author.guild_permissions.administrator}")
  38. #print(f"Author has perms in main output channel: {self.bot.get_channel(self.bot.OUTPUT_CHANNEL_ID).permissions_for(ctx.author).send_messages}")
  39. #print(f"Has author perms in main output channel: {ctx.author.permissions_in(self.bot.get_channel(self.bot.OUTPUT_CHANNEL_ID)).send_messages}")
  40. if not message:
  41. if channel:
  42. await ctx.respond(f"What would you like me to say in `{channel}`?")
  43. elif user:
  44. await ctx.respond(f"What would you like me to say to `{user}`?")
  45. else:
  46. await ctx.respond("What would you like me to say?")
  47. elif channel:
  48. 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:
  49. await channel.send(message)
  50. await ctx.respond("Message sent.")
  51. await report(self.bot, f"`{ctx.author}` @ {channel.mention}: {message}", ctx.guild)
  52. else:
  53. await ctx.respond(f"Interactive mode for {channel} is deactivated.")
  54. elif user and self.bot.get_channel(self.bot.OUTPUT_CHANNEL_ID).permissions_for(ctx.author).send_messages:
  55. await user.send(message)
  56. await ctx.respond("Message sent.")
  57. await report(self.bot, f"`{ctx.author}` @ `{user.name}`: {message}")
  58. else:
  59. await ctx.respond(message)
  60. await report(self.bot, f"`{ctx.author}` has sent {message} locally.", ctx.guild)
  61. @commands.slash_command(
  62. description="Change status.",
  63. brief="Set status",
  64. help="Update the bot's status."
  65. )
  66. async def status(self, ctx: commands.Context, *, text: str):
  67. # Halt on ignore list.
  68. if await check_ignore(self.bot.pg, ctx.author):
  69. return
  70. await self.bot.change_presence(activity=discord.Game(name=text))
  71. await report(self.bot, f"`{ctx.author}` has set my status to `{text}`.")
  72. @commands.slash_command(
  73. description="Get ignored.",
  74. brief="Ignore sender",
  75. help="Will have the bot ignore the user from now on."
  76. )
  77. async def ignoreme(self, ctx: commands.Context):
  78. # Halt on ignore list.
  79. if await check_ignore(self.bot.pg, ctx.author):
  80. return
  81. await ignore_user(self.bot.pg, ctx.author.id)
  82. await ctx.respond("To revert this use the `/unignoreme` command.")
  83. await report(self.bot, f"`{ctx.author}` has requested to be ignored.")
  84. @commands.slash_command(
  85. description="No longer get ingored.",
  86. brief="Un-ignore sender",
  87. help="No longer will the bot ignore the user."
  88. )
  89. async def unignoreme(self, ctx: commands.Context):
  90. await unignore_user(self.bot.pg, ctx.author.id)
  91. await ctx.respond(f"I shall now interact with you again where my channel settings allow it.")
  92. await report(self.bot, f"`{ctx.author}` has requested to be un-ignored.")
  93. @commands.slash_command(
  94. description="Ignore status for user.",
  95. brief="Check if user is ingored",
  96. help="Verify if the user is being ignored."
  97. )
  98. async def isignored(self, ctx: commands.Context, user: Optional[discord.User]):
  99. # Halt on ignore list.
  100. if await check_ignore(self.bot.pg, ctx.author):
  101. return
  102. if not user:
  103. user = ctx.author
  104. if await is_ignored(self.bot.pg, user.id):
  105. await ctx.respond(f"I am ingoring `{user}`.")
  106. else:
  107. await ctx.respond(f"I am not ignoring `{user}`.")
  108. def setup(bot): # Called by Pycord to setup the cog
  109. bot.add_cog(Admin(bot)) # Add the cog to the bot