1
0

general.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 common.logging import report
  7. def setup(bot: commands.Bot):
  8. bot.add_cog(General(bot))
  9. class General(commands.Cog):
  10. """General functionality."""
  11. def __init__(self, bot: commands.Bot):
  12. self.bot = bot
  13. @commands.command(
  14. description="Get the bot's current websocket and API latency.",
  15. brief="Test latency",
  16. help="Test latency by polling the gateway and API."
  17. )
  18. async def ping(self, ctx: commands.Context):
  19. start_time = time.time()
  20. message = await ctx.send(f"Pong!\nGateway heartbeat in {round(self.bot.latency * 1000)}ms.")
  21. end_time = time.time()
  22. await ctx.send(f"API roundtrip latency {round((end_time - start_time) * 1000)}ms.")
  23. @commands.command(
  24. description="Rubbish information.",
  25. brief="Get info",
  26. help="Display some crap."
  27. )
  28. async def info(self, ctx: commands.Context):
  29. """Displays some rubbish info."""
  30. embed = discord.Embed(title=f"Guild: {ctx.guild}.")
  31. await ctx.send(embed=embed)
  32. #await ctx.send(f"Guild: {ctx.guild}.")
  33. @commands.command(
  34. description="Send a message",
  35. brief="Chat",
  36. help="Make a chat message",
  37. aliases= ("say", "pm", "dm", "echo", "print")
  38. )
  39. async def msg(self, ctx: commands.Context, channel: Optional[discord.TextChannel], user: Optional[discord.User], *, message: str = None):
  40. if not message:
  41. if channel:
  42. await ctx.send(f"What would you like me to say in {channel}?")
  43. elif user:
  44. await ctx.send(f"What would you like me to say to {user}?")
  45. else:
  46. await ctx.send("What would you like me to say?")
  47. elif channel:
  48. if await get_interact(self.bot.pg, channel.id): # or await ctx.author.get_guild_permissions(channel.guild):
  49. await channel.send(message)
  50. await report(f"{ctx.author} has sent {message} to {channel.name}")
  51. else:
  52. await ctx.send(f"Interactive mode for {channel} is deactivated.")
  53. elif user:
  54. await user.send(message)
  55. await report(f"{ctx.author} has sent {message} to {user.name}")
  56. else:
  57. await ctx.send(message)
  58. await report(self.bot, f"`{ctx.author}` has sent `{message}` locally.")
  59. @commands.command(
  60. description="Change status.",
  61. brief="Set status",
  62. help="Update the bot's status."
  63. )
  64. async def status(self, ctx: commands.Context, *, text: str):
  65. await self.bot.change_presence(activity=discord.Game(name=text))