async def defaulttime(ctx, time): """ This command sets the delay of when the bot should post. It will only get 25 posts max, but some reddits are slow TIMES MUST BE: 5m/10m/15m/30m/45m/1h I have an upper limit of an hour because if it gets more than 25 posts, that would take a very long time. Usage: r/default delay 10m Permissions required: Administrator :param ctx: :param time: :return: """ sid = ctx.message.server.id if time == '5m': data[sid]['delay'] = 300 await bot.say(f'The delay has changed to {time}.') elif time == '10m': data[sid]['delay'] = 600 await bot.say(f'The delay has changed to {time}.') elif time == '15m': data[sid]['delay'] = 900 await bot.say(f'The delay has changed to {time}.') elif time == '30m': data[sid]['delay'] = 1800 await bot.say(f'The delay has changed to {time}.') elif time == '45m': data[sid]['delay'] = 2700 await bot.say(f'The delay has changed to {time}.') elif time == '1h': data[sid]['delay'] = 3600 await bot.say(f'The delay has changed to {time}.') else: await bot.say('Sorry time must be 5m/10m/15m/30m/45m/1h') fmtjson.edit_json('options', data) changedefault(ctx)
async def unsub(ctx, *subreddit): """ This command will 'unsubscribe' from a reddit and will no longer make posts. Usage: r/unsub <subreddit> Ex. r/unsub news funny husky Permissions required: Administrator :param ctx: :param subreddit: :return: """ sid = ctx.message.server.id subs = data[sid]['watching'] removed = [] for reddit in subreddit: if reddit in subs: subs.remove(reddit.lower()) removed.append(reddit.lower()) else: await bot.say( f'Subreddit: {reddit} not found. Please make sure you are spelling' f' it correctly.') if removed: data[sid]['watching'] = subs await bot.say(f"Subreddit(s): {', '.join(removed)} removed!\n" f"You will notice this change when I scour reddit again." ) fmtjson.edit_json('options', data) commandinfo(ctx)
async def offjoin(servers): """ This is for if the bot is offline and joins a server. :param servers: :return: """ for server in servers: if not server.id in data.keys(): data.update({ server.id: { 'default_channel': server.owner.id, 'NSFW_channel': '', 'id': server.id, 'watching': [], 'NSFW_filter': 1, 'create_channel': 0, 'delay': 300 } }) fmtjson.edit_json('options', data) await bot.send_message( server.owner, 'Thanks for adding me to the server! There are a few things I need ' 'from you or your admins to get running though.\n' 'In the discord server(NOT HERE),Please set the default channel for me to ' 'post in, or turn on the option for me to create a channel for each ' 'subreddit. `r/default channel general` or `r/default create`\n' 'Right now I have the default channel set to PM you, so *I would ' 'suggest changing this*. After that, you or your admins ' 'can run `r/sub funny` and let the posts flow in!')
async def on_server_remove(server): """ When a bot leaves/gets kicked, remove the server from the .json file. :param server: :return: """ data.pop(server.id, None) fmtjson.edit_json("options", data)
async def removeall(ctx): """ This command will "unsubscribe" from all reddits. Usage: r/removeall Permissions required: Administrator :param ctx: :return: """ sid = ctx.message.server.id data[sid]['watching'] = [] fmtjson.edit_json('options', data) await bot.say( 'You are no longer subbed to any subreddits! Please don\'t get rid of me. :[' )
async def defaultall(ctx): """ This command sets all options to their default. Usage: r/default all Permissions required: Administrator :param ctx: :return: """ sid = ctx.message.server.id data[sid]['default_channel'] = ctx.message.server.owner.id data[sid]['NSFW_channel'] = '' data[sid]['delay'] = 300 data[sid]['NSFW_filter'] = 1 data[sid]['create_channel'] = 0 data[sid]['watching'] = [] fmtjson.edit_json('options', data) await bot.say( 'All options have been set to their default. Default channel is the server owner, so please use' '`r/default channel <channel name>` EX.`r/default channel general`')
async def offremove(servers): """ This is for if the bot gets kicked while offline :param servers: :return: """ serverlist = [] removed = [] for server in servers: serverlist.append(server.id) for key in data: if not key in serverlist: removed.append(key) if removed: for server in removed: data.pop(server, None) fmtjson.edit_json('options', data)
async def on_server_join(server): """ When the bot joins a server, it will set defaults in the json file and pull all info it needs. defaults: default channel == 'server owner' nsfw channel == '' id == server id delay == 300 (5 minutes) nsfw filter == 1 create channel == 0 watching == [] :param server: :return: """ data.update({ server.id: { 'default_channel': server.owner.id, 'NSFW_channel': '', 'id': server.id, 'delay': 300, 'NSFW_filter': 1, 'create_channel': 0, 'watching': [] } }) fmtjson.edit_json('options', data) # message owner about bot usage. await bot.send_message( server.owner, 'Thanks for adding me to the server! There are a few things I need ' 'from you or your admins to get running though.\n' 'In the discord server(NOT HERE),Please set the default channel for me to ' 'post in, or turn on the option for me to create a channel for each ' 'subreddit. `r/default channel general` or `r/default create`\n' 'Right now I have the default channel set to PM you, so *I would ' 'suggest changing this*. After that, you or your admins ' 'can run `r/sub funny` and let the posts flow in!') # create new task for the server asyncio.ensure_future(my_background_task(server.id))
async def createChannels(ctx): ''' Toggles the create channels option. DEFAULT: OFF Usage: r/default create Permissions required: Administrator :param ctx: :return: ''' sid = ctx.message.server.id if data[sid]['create_channel'] == 1: data[sid]['create_channel'] = 0 await bot.say( "Creating channels has been TURNED OFF. I will now make all of my posts in " "your default channel.") else: data[sid]['create_channel'] = 1 await bot.say( "Creating channels has been TURNED ON. I can now create channels for each reddit " "that you are watching.") fmtjson.edit_json('options', data) changedefault(ctx)
async def subscribe(ctx, *subreddit): """ This command will 'subscribe' to a reddit and will make posts from it. Usage: r/sub <subreddit> Ex. r/sub news funny husky Permissions required: Administrator :param ctx: :param subreddit: :return: """ sid = ctx.message.server.id subs = data[sid]['watching'] added = [] for reddit in subreddit: url = f"https://www.reddit.com/r/{reddit}/new/.json" posts = await respcheck(url) if posts: if reddit.lower() in subs: await bot.say(f'{reddit} is already in your list!') continue else: subs.append(reddit.lower()) added.append(reddit.lower()) else: await bot.say( f'Sorry, I can\'t reach {reddit}. ' f'Check your spelling or make sure that the reddit actually exists.' ) if added: data[sid]['watching'] = subs await bot.say(f"Subreddit(s): {', '.join(added)} added!\n" f"You will notice this change when I scour reddit again." ) fmtjson.edit_json('options', data) commandinfo(ctx)
async def nsfwFilter(ctx): ''' Toggles the NSFW filter. DEFAULT: ON Usage: r/default nsfw Permissions required: Administrator :param ctx: :return: ''' sid = ctx.message.server.id if data[sid]['NSFW_filter'] == 1: data[sid]['NSFW_filter'] = 0 await bot.say( "NSFW filter has been TURNED OFF. Enjoy your sinful images, loser. Also be sure" "to label your default channel or the NSFW reddit channels as NSFW channels." ) else: data[sid]['NSFW_filter'] = 1 await bot.say( "NSFW filter has been TURNED ON. I really don't like looking for those " "images.") fmtjson.edit_json('options', data) changedefault(ctx)
async def defaultChannel(ctx, channel): """ Set the Default nsfwchannel for the bot to post in. Usage: r/default nsfwchannel <channel> Permissions required: Administrator :param ctx: :param channel: :return: """ newchannel = discord.utils.get(bot.get_all_channels(), name=channel, server__id=ctx.message.server.id) if not newchannel: raise commands.CommandInvokeError sid = ctx.message.server.id data[sid]['NSFW_channel'] = newchannel.id await bot.say(f"NSFW default channel changed to {newchannel.mention}\n" f"You will notice this change when I scour reddit again.") fmtjson.edit_json('options', data) changedefault(ctx)