general.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 local_settings import COMMAND_PREFIX, OUTPUT_CHANNEL
  9. from common.settings import check_ignore
  10. async def setup(bot: commands.Bot):
  11. await bot.add_cog(GeneralCommands(bot))
  12. class GeneralCommands(commands.Cog):
  13. """General functionality."""
  14. def __init__(self, bot: commands.Bot):
  15. self.bot = bot
  16. @commands.command(
  17. description="Get the bot's current websocket and API latency.",
  18. brief="Test latency",
  19. help="Test latency by polling the gateway and API."
  20. )
  21. async def ping(self, ctx: commands.Context):
  22. # Halt on ignore list.
  23. if await check_ignore(self.bot.pg, ctx.author):
  24. return
  25. start_time = time.time()
  26. message = await ctx.send(f"Pong!\nGateway heartbeat in {round(self.bot.latency * 1000)}ms.")
  27. end_time = time.time()
  28. await ctx.send(f"API roundtrip latency {round((end_time - start_time) * 1000)}ms.")
  29. @commands.command(
  30. description="Rubbish information.",
  31. brief="Get info",
  32. help="Display some crap."
  33. )
  34. async def info(self, ctx: commands.Context):
  35. # Halt on ignore list.
  36. if await check_ignore(self.bot.pg, ctx.author):
  37. return
  38. """Displays some rubbish info."""
  39. embed = discord.Embed(title=f"Guild: {ctx.guild}.")
  40. await ctx.send(embed=embed)
  41. #await ctx.send(f"Guild: {ctx.guild}.")
  42. @commands.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. # Halt on ignore list.
  50. if await check_ignore(self.bot.pg, ctx.author):
  51. return
  52. #print(ctx.author.permissions_in(self.bot.get_channel(OUTPUT_CHANNEL)).send_messages)
  53. if not message:
  54. if channel:
  55. await ctx.send(f"What would you like me to say in `{channel}`?")
  56. elif user:
  57. await ctx.send(f"What would you like me to say to `{user}`?")
  58. else:
  59. await ctx.send("What would you like me to say?")
  60. elif channel:
  61. if await get_interact(self.bot.pg, channel.id) or ctx.author.permissions_in(self.bot.get_channel(OUTPUT_CHANNEL)).send_messages:
  62. await channel.send(message)
  63. await report(self.bot, f"`{ctx.author}` @ {channel.mention}: {message}", ctx.guild)
  64. else:
  65. await ctx.send(f"Interactive mode for {channel} is deactivated.")
  66. elif user and ctx.author.permissions_in(self.bot.get_channel(OUTPUT_CHANNEL)).send_messages:
  67. await user.send(message)
  68. await report(self.bot, f"`{ctx.author}` has sent {message} to `{user.name}`")
  69. else:
  70. await ctx.send(message)
  71. await report(self.bot, f"`{ctx.author}` has sent {message} locally.", ctx.guild)
  72. @commands.command(
  73. description="Change status.",
  74. brief="Set status",
  75. help="Update the bot's status."
  76. )
  77. async def status(self, ctx: commands.Context, *, text: str):
  78. # Halt on ignore list.
  79. if await check_ignore(self.bot.pg, ctx.author):
  80. return
  81. await self.bot.change_presence(activity=discord.Game(name=text))
  82. await report(self.bot, f"`{ctx.author}` has set my status to `{text}`.")
  83. @commands.command(
  84. description="Get ignored.",
  85. brief="Ignore sender",
  86. help="Will have the bot ignore the user from now on."
  87. )
  88. async def ignoreme(self, ctx: commands.Context):
  89. # Halt on ignore list.
  90. if await check_ignore(self.bot.pg, ctx.author):
  91. return
  92. await ignore_user(self.bot.pg, ctx.author.id)
  93. await ctx.send(f"To revert this use the `{COMMAND_PREFIX}unignoreme` command.")
  94. await report(self.bot, f"`{ctx.author}` has requested to be ignored.")
  95. @commands.command(
  96. description="No longer get ingored.",
  97. brief="Un-ignore sender",
  98. help="No longer will the bot ignore the user."
  99. )
  100. async def unignoreme(self, ctx: commands.Context):
  101. await unignore_user(self.bot.pg, ctx.author.id)
  102. await ctx.send(f"I shall now interact with you again where my channel settings allow it.")
  103. await report(self.bot, f"`{ctx.author}` has requested to be un-ignored.")
  104. @commands.command(
  105. description="Ignore status for user.",
  106. brief="Check if user is ingored",
  107. help="Verify if the user is being ignored."
  108. )
  109. async def isignored(self, ctx: commands.Context, user: Optional[discord.User]):
  110. # Halt on ignore list.
  111. if await check_ignore(self.bot.pg, ctx.author):
  112. return
  113. if not user:
  114. user = ctx.author
  115. if await is_ignored(self.bot.pg, user.id):
  116. await ctx.send(f"I am ingoring `{user}`.")
  117. else:
  118. await ctx.send(f"I am not ignoring `{user}`.")