diff --git a/bot/modules/topgg/commands.py b/bot/modules/topgg/commands.py new file mode 100644 index 00000000..f473fbe0 --- /dev/null +++ b/bot/modules/topgg/commands.py @@ -0,0 +1,75 @@ +import discord +from .module import module +from wards import guild_admin +from bot.cmdClient.checks.global_perms import in_guild +from settings.user_settings import UserSettings + +from .webhook import on_dbl_vote + +@module.cmd( + "forcevote", + desc="Simulate Topgg Vote.", + group="Guild Admin", + aliases=('debugvote', 'topggvote') +) +@guild_admin() +async def cmd_forcevote(ctx): + """ + Usage``: + {prefix}forcevote + Description: + Simulate Topgg Vote without actually a confirmation from Topgg site. + + Can be used for force a vote for testing or if topgg has an error or production time bot error. + """ + target = ctx.author + # Identify the target + if ctx.args: + if not ctx.msg.mentions: + return await ctx.error_reply("Please mention a user to simulate a vote!") + target = ctx.msg.mentions[0] + + await on_dbl_vote({"user": target.id, "type": "test"}) + return await ctx.reply('Topgg vote simulation successful on {}'.format(target)) + + +@module.cmd( + "vote", + desc="Get Top.gg bot's link for Economy boost.", + group="Economy", + aliases=('topgg', 'topggvote', 'upvote') +) +@in_guild() +async def cmd_vote(ctx): + """ + Usage``: + {prefix}vote + Description: + Get Top.gg bot's link for +25% Economy boost. + """ + target = ctx.author + + embed=discord.Embed( + title="Topgg Upvote", + description='Please click [here](https://top.gg/bot/889078613817831495/vote) to upvote.\n\nThanks.', + colour=discord.Colour.orange() + ).set_thumbnail( + url="https://cdn.discordapp.com/attachments/908283085999706153/930851470994182144/lionlogo.png" + ) + return await ctx.reply(embed=embed) + + +@module.cmd( + "vote_remainder", + group="Personal Settings", + desc="Turn on/off DM Remainder to Upvote me." +) +async def cmd_remind_vote(ctx): + """ + Usage``: + {prefix}vote_remainder on + {prefix}vote_remainder off + + Use this setting to enable/disable DM remainders from me to upvote on Top.gg. + """ + await UserSettings.settings.vote_remainder.command(ctx, ctx.author.id) diff --git a/bot/modules/topgg/settings.py b/bot/modules/topgg/settings.py new file mode 100644 index 00000000..e60595b3 --- /dev/null +++ b/bot/modules/topgg/settings.py @@ -0,0 +1,45 @@ +from settings.user_settings import UserSettings, UserSetting +from settings.setting_types import Boolean + +from modules.reminders.reminder import Reminder +from modules.reminders.data import reminders + +from .utils import * + +@UserSettings.attach_setting +class topgg_vote_remainder(Boolean, UserSetting): + attr_name = 'vote_remainder' + _data_column = 'remaind_upvote' + + _default = True + + display_name = 'Upvote Remainder' + desc = "Turn on/off DM Remainders to Upvote me." + long_desc = "Use this setting to enable/disable DM remainders from me to upvote on Top.gg." + + @property + def success_response(self): + if self.value: + # Check if reminder is already running + create_remainder(self.id) + + return ( + "Remainder, in your DMs, to upvote me are {}.\n\n" + "`Do make sure that I can DM you.`" + ).format(self.formatted) + else: + # Check if reminder is already running and get its id + r = reminders.select_one_where( + userid=self.id, + select_columns='reminderid', + content=remainder_content, + _extra="ORDER BY remind_at DESC LIMIT 1" + ) + + # Cancel and delete Remainder if already running + if r: + Reminder.delete(r['reminderid']) + + return ( + "Remainder, in your DMs, to upvote me are Off." + ).format(self.formatted) \ No newline at end of file diff --git a/bot/modules/topgg/utils.py b/bot/modules/topgg/utils.py new file mode 100644 index 00000000..f50c6da5 --- /dev/null +++ b/bot/modules/topgg/utils.py @@ -0,0 +1,72 @@ +from email.mime import image +import discord +import datetime +from meta.client import client +from bot.settings.setting_types import Integer +from meta import sharding + +from modules.reminders.reminder import Reminder +from modules.reminders.data import reminders + +from . import data as db +from data.conditions import GEQ + +topgg_upvote_link = 'https://top.gg/bot/889078613817831495/vote' +remainder_content = "You can now Upvote me again in Top.gg. \nMy Upvote link is {}".format(topgg_upvote_link) + +# Will return None if user has not voted in [-12.5hrs till now] +# else will return a Tuple containing timestamp of when exactly she voted +def get_last_voted_timestamp(userid: Integer): + return db.topggvotes.select_one_where( + userid=userid, + select_columns="boostedTimestamp", + boostedTimestamp=GEQ(datetime.datetime.utcnow() - datetime.timedelta(hours=12.5)), + _extra="ORDER BY boostedTimestamp DESC LIMIT 1" + ) + +# Checks if a remainder is already running (immaterial of remind_at time) +# If no remainder exists creates a new remainder and schedules it +def create_remainder(userid): + if not reminders.select_one_where( + userid=userid, + content=remainder_content, + _extra="ORDER BY remind_at DESC LIMIT 1" + ): + last_vote_time = get_last_voted_timestamp(userid) + + # if no, Create reminder + reminder = Reminder.create( + userid=userid, + content=remainder_content, + message_link=None, + interval=None, + #remind_at=datetime.datetime.utcnow() + datetime.timedelta(minutes=2) + remind_at=last_vote_time[0] + datetime.timedelta(hours=12.5) if last_vote_time else datetime.datetime.utcnow() + datetime.timedelta(minutes=5) + ) + + # Schedule reminder + if sharding.shard_number == 0: + reminder.schedule() + + +async def send_user_dm(userid): + # Send the message, if possible + if not (user := client.get_user(userid)): + try: + user = await client.fetch_user(userid) + except discord.HTTPException: + pass + if user: + try: + embed=discord.Embed( + title="Thankyou.", + description='Thankyou for upvoting.', + colour=discord.Colour.orange() + ).set_image( + url="https://cdn.discordapp.com/attachments/908283085999706153/930559064323268618/unknown.png" + ) + + await user.send(embed=embed) + except discord.HTTPException: + # Nothing we can really do here. Maybe tell the user about their reminder next time? + pass