You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
7.1KB

  1. # -*- coding: utf-8 -*-
  2. # MIT License
  3. #
  4. # Copyright (c) 2017-2018 Roxanne Gibson
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in all
  14. # copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. import typing
  24. import discord
  25. from discord.ext import commands
  26. import roxbot
  27. from roxbot.db import *
  28. class JoinLeaveSingle(db.Entity):
  29. greets_enabled = Required(bool, default=False)
  30. goodbyes_enabled = Required(bool, default=False)
  31. greets_channel_id = Optional(int, nullable=True, size=64)
  32. goodbyes_channel_id = Optional(int, nullable=True, size=64)
  33. greets_custom_message = Optional(str, nullable=True)
  34. guild_id = Required(int, size=64, unique=True)
  35. class JoinLeave(commands.Cog):
  36. """JoinLeave is a cog that allows you to create custom welcome and goodbye messages for your Discord server. """
  37. DEFAULT_MESSAGE = "Be sure to read the rules."
  38. def __init__(self, bot_client):
  39. self.bot = bot_client
  40. self.autogen_db = JoinLeaveSingle
  41. @commands.Cog.listener()
  42. async def on_member_join(self, member):
  43. """
  44. Greets users when they join a server.
  45. """
  46. if member == self.bot.user:
  47. return
  48. with db_session:
  49. settings = JoinLeaveSingle.get(guild_id=member.guild.id)
  50. if not settings.greets_enabled:
  51. return
  52. message = settings.greets_custom_message or self.DEFAULT_MESSAGE
  53. em = discord.Embed(
  54. title="Welcome to {}!".format(member.guild),
  55. description='Hey {}! Welcome to **{}**! {}'.format(member.mention, member.guild, message),
  56. colour=roxbot.EmbedColours.pink)
  57. em.set_thumbnail(url=member.avatar_url)
  58. channel = member.guild.get_channel(settings.greets_channel_id)
  59. return await channel.send(embed=em)
  60. @commands.Cog.listener()
  61. async def on_member_remove(self, member):
  62. """
  63. The same but the opposite
  64. """
  65. if member == self.bot.user:
  66. return
  67. with db_session:
  68. settings = JoinLeaveSingle.get(guild_id=member.guild.id)
  69. if settings.goodbyes_enabled:
  70. try:
  71. channel = member.guild.get_channel(settings.goodbyes_channel_id)
  72. return await channel.send(embed=discord.Embed(
  73. description="{}#{} has left or been beaned.".format(member.name, member.discriminator), colour=roxbot.EmbedColours.pink))
  74. except AttributeError:
  75. pass
  76. @commands.Cog.listener()
  77. async def on_guild_channel_delete(self, channel):
  78. """Cleans up settings on removal of stored IDs."""
  79. with db_session:
  80. settings = JoinLeaveSingle.get(guild_id=channel.guild.id)
  81. if channel.id == settings.greets_channel_id:
  82. settings.greets_channel_id = None
  83. if channel.id == settings.goodbyes_channel_id:
  84. settings.goodbyes_channel_id = None
  85. @commands.guild_only()
  86. @commands.has_permissions(manage_messages=True)
  87. @commands.command()
  88. async def greets(self, ctx, setting, channel: typing.Optional[discord.TextChannel] = None, *, text = ""):
  89. """Edits settings for the Welcome Messages
  90. Options:
  91. enable/disable: Enable/disables greet messages.
  92. channel: Sets the channel for the message to be posted in. If no channel is provided, it will default to the channel the command is executed in.
  93. message: Specifies a custom message for the greet messages.
  94. Example:
  95. Enable greet messages, set the channel to the current one, and set a custom message to be appended.
  96. `;greets enable`
  97. `;greets message "Be sure to read the rules and say hi! :wave:"`
  98. `;greets channel` # if no channel is provided, it will default to the channel the command is executed in.
  99. """
  100. setting = setting.lower()
  101. with db_session:
  102. settings = JoinLeaveSingle.get(guild_id=ctx.guild.id)
  103. if setting == "enable":
  104. settings.greets_enabled = True
  105. await ctx.send("'greets' was enabled!")
  106. elif setting == "disable":
  107. settings.greets_enabled = False
  108. await ctx.send("'greets' was disabled :cry:")
  109. elif setting in ("channel", "greet-channel"):
  110. channel = channel or ctx.channel
  111. settings.greets_channel_id = channel.id
  112. await ctx.send("Set greets channel to {}".format(channel.mention))
  113. elif setting in ("message", "custom-message"):
  114. settings.greets_custom_message = text
  115. await ctx.send("Custom message set to '{}'".format(text))
  116. else:
  117. return await ctx.send("No valid option given.")
  118. @commands.guild_only()
  119. @commands.has_permissions(manage_messages=True)
  120. @commands.command()
  121. async def goodbyes(self, ctx, setting, *, channel: typing.Optional[discord.TextChannel] = None):
  122. """Edits settings for the goodbye messages.
  123. Options:
  124. enable/disable: Enable/disables goodbye messages.
  125. channel: Sets the channel for the message to be posted in. If no channel is provided, it will default to the channel the command is executed in.
  126. Example:
  127. Enable goodbye messages, set the channel one called `#logs`
  128. `;goodbyes enable`
  129. `;goodbyes channel #logs`
  130. """
  131. setting = setting.lower()
  132. with db_session:
  133. settings = JoinLeaveSingle.get(guild_id=ctx.guild.id)
  134. if setting == "enable":
  135. settings.goodbyes_enabled = True
  136. await ctx.send("'goodbyes' was enabled!")
  137. elif setting == "disable":
  138. settings.goodbyes_enabled = False
  139. await ctx.send("'goodbyes' was disabled :cry:")
  140. elif setting in ("channel", "goodbye-channel"):
  141. channel = channel or ctx.channel
  142. settings.goodbyes_channel_id = channel.id
  143. await ctx.send("Set goodbye channel to {}".format(channel.mention))
  144. else:
  145. return await ctx.send("No valid option given.")
  146. def setup(bot_client):
  147. bot_client.add_cog(JoinLeave(bot_client))