general.py 2.1 KB

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