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