Browse Source

Coding cleaning and weird bug

tags/v0.3.5
roxie 7 years ago
parent
commit
ae81dd205c
7 changed files with 111 additions and 98 deletions
  1. +23
    -19
      cogs/Admin.py
  2. +19
    -18
      cogs/Twitch.py
  3. +3
    -2
      cogs/__init__.py
  4. +43
    -0
      cogs/joinLeave.py
  5. +17
    -17
      cogs/selfAssign.py
  6. +6
    -6
      config/config.py
  7. +0
    -36
      main.py

+ 23
- 19
cogs/Admin.py View File

def __init__(self, Bot): def __init__(self, Bot):
self.bot = Bot self.bot = Bot
self.con = Config(Bot) self.con = Config(Bot)
self.serverconfig = self.con.serverconfig


@bot.command(pass_context=True, hidden=True) @bot.command(pass_context=True, hidden=True)
async def blacklist(self, ctx, option, *args): async def blacklist(self, ctx, option, *args):
if not owner(ctx): if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after) return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
else: else:
if setting in self.con.serverconfig[ctx.message.server.id]:
self.con.serverconfig = self.con.load_config()
if not self.con.serverconfig[ctx.message.server.id][setting]["enabled"]:
self.con.serverconfig[ctx.message.server.id][setting]["enabled"] = 1
self.con.updateconfig()
server_id = ctx.message.server.id
if setting in self.serverconfig[server_id]:
self.serverconfig = self.con.load_config()
if not self.serverconfig[server_id][setting]["enabled"]:
self.serverconfig[server_id][setting]["enabled"] = 1
self.con.updateconfig(self.serverconfig)
return await self.bot.say("'{}' was enabled!".format(setting)) return await self.bot.say("'{}' was enabled!".format(setting))
else: else:
self.con.serverconfig[ctx.message.server.id][setting]["enabled"] = 0
self.con.updateconfig()
self.serverconfig[server_id][setting]["enabled"] = 0
self.con.updateconfig(self.serverconfig)
return await self.bot.say("'{}' was disabled :cry:".format(setting)) return await self.bot.say("'{}' was disabled :cry:".format(setting))
else: else:
return await self.bot.say("That module dont exist fam. You made the thing") return await self.bot.say("That module dont exist fam. You made the thing")


# TODO: Combine all set commands into one

@bot.command(pass_context=True, hidden=True) @bot.command(pass_context=True, hidden=True)
async def set_welcomechannel(self, ctx, channel: discord.Channel = None): async def set_welcomechannel(self, ctx, channel: discord.Channel = None):
if not owner(ctx): if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after) return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
else: else:
self.con.serverconfig = self.con.load_config()
self.con.serverconfig[ctx.message.server.id]["greets"]["welcome-channel"] = channel.id
self.con.updateconfig()
self.serverconfig = self.con.load_config()
self.serverconfig[ctx.message.server.id]["greets"]["welcome-channel"] = channel.id
self.con.updateconfig(self.serverconfig)
return await self.bot.say("{} has been set as the welcome channel!".format(channel.mention)) return await self.bot.say("{} has been set as the welcome channel!".format(channel.mention))


@bot.command(pass_context=True, hidden=True) @bot.command(pass_context=True, hidden=True)
if not owner(ctx): if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after) return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
else: else:
self.con.serverconfig = self.con.load_config()
self.con.serverconfig[ctx.message.server.id]["goodbyes"]["goodbye-channel"] = channel.id
self.con.updateconfig()
self.serverconfig = self.con.load_config()
self.serverconfig[ctx.message.server.id]["goodbyes"]["goodbye-channel"] = channel.id
self.con.updateconfig(self.serverconfig)
return await self.bot.say("{} has been set as the goodbye channel!".format(channel.mention)) return await self.bot.say("{} has been set as the goodbye channel!".format(channel.mention))


@bot.command(pass_context=True, hidden=True) @bot.command(pass_context=True, hidden=True)
if not owner(ctx): if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after) return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
else: else:
self.con.serverconfig = self.con.load_config()
self.con.serverconfig[ctx.message.server.id]["twitch"]["twitch-channel"] = channel.id
self.con.updateconfig()
self.serverconfig = self.con.load_config()
self.serverconfig[ctx.message.server.id]["twitch"]["twitch-channel"] = channel.id
self.con.updateconfig(self.serverconfig)
return await self.bot.say("{} has been set as the twitch shilling channel!".format(channel.mention)) return await self.bot.say("{} has been set as the twitch shilling channel!".format(channel.mention))


@bot.command(pass_context=True, hidden=True) @bot.command(pass_context=True, hidden=True)
if not owner(ctx): if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after) return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
else: else:
self.con.serverconfig = self.con.load_config()
self.con.serverconfig[ctx.message.server.id]["greets"]["custom-message"] = message
self.con.updateconfig()
self.serverconfig = self.con.load_config()
self.serverconfig[ctx.message.server.id]["greets"]["custom-message"] = message
self.con.updateconfig(self.serverconfig)
return await self.bot.say("Custom message set to '{}'".format(message)) return await self.bot.say("Custom message set to '{}'".format(message))


@bot.command(pass_context=True, hidden=True, aliases=["setava"]) @bot.command(pass_context=True, hidden=True, aliases=["setava"])

+ 19
- 18
cogs/Twitch.py View File

def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self.con = Config(bot) self.con = Config(bot)
self.serverconfig = self.con.serverconfig


async def on_member_update(self, member_b, member_a): async def on_member_update(self, member_b, member_a):
# Twitch Shilling Part # Twitch Shilling Part
return return
if member_a.game: if member_a.game:
if member_a.game.type and not member_b.game.type: if member_a.game.type and not member_b.game.type:
ts_enabled = self.con.serverconfig[member_a.server.id]["twitch"]["enabled"]
ts_whitelist = self.con.serverconfig[member_a.server.id]["twitch"]["whitelist"]["enabled"]
ts_enabled = self.serverconfig[member_a.server.id]["twitch"]["enabled"]
ts_whitelist = self.serverconfig[member_a.server.id]["twitch"]["whitelist"]["enabled"]
if ts_enabled: if ts_enabled:
if not ts_whitelist or member_a.id in \ if not ts_whitelist or member_a.id in \
self.con.serverconfig[member_a.server.id]["twitch"]["whitelist"]["list"]:
channel = discord.Object(self.con.serverconfig[member_a.server.id]["twitch"]["twitch-channel"])
self.serverconfig[member_a.server.id]["twitch"]["whitelist"]["list"]:
channel = discord.Object(self.serverconfig[member_a.server.id]["twitch"]["twitch-channel"])
return await self.bot.send_message(channel, return await self.bot.send_message(channel,
content=":video_game:** {} is live!** :video_game:\n{}\n{}".format( content=":video_game:** {} is live!** :video_game:\n{}\n{}".format(
member_a.name, member_a.game.name, member_a.game.url)) member_a.name, member_a.game.name, member_a.game.url))
if not owner(ctx): if not owner(ctx):
return await self.bot.reply("You do not have permission to do this command.", delete_after=20) return await self.bot.reply("You do not have permission to do this command.", delete_after=20)
else: else:
self.con.serverconfig = self.con.load_config()
if not self.con.serverconfig[ctx.server.id]["twitch"]["whitelist"]["enabled"]:
self.con.serverconfig[ctx.server.id]["twitch"]["whitelist"]["enabled"] = 1
self.con.updateconfig()
self.serverconfig = self.con.load_config()
if not self.serverconfig[ctx.server.id]["twitch"]["whitelist"]["enabled"]:
self.serverconfig[ctx.server.id]["twitch"]["whitelist"]["enabled"] = 1
self.con.updateconfig(self.serverconfig)
return await self.bot.reply("Whitelist for Twitch shilling has been enabled.") return await self.bot.reply("Whitelist for Twitch shilling has been enabled.")
else: else:
self.con.serverconfig[ctx.server.id]["twitch"]["whitelist"]["enabled"] = 0
self.con.updateconfig()
self.serverconfig[ctx.server.id]["twitch"]["whitelist"]["enabled"] = 0
self.con.updateconfig(self.serverconfig)
return await self.bot.reply("Whitelist for Twitch shilling has been disabled.") return await self.bot.reply("Whitelist for Twitch shilling has been disabled.")


@bot.command(pass_context=True, hidden=True) @bot.command(pass_context=True, hidden=True)
return await self.bot.say('Invalid option "%s" specified, use +, -, add, or remove' % option, expire_in=20) return await self.bot.say('Invalid option "%s" specified, use +, -, add, or remove' % option, expire_in=20)


if option in ['+', 'add']: if option in ['+', 'add']:
self.con.serverconfig = self.con.load_config()
self.serverconfig = self.con.load_config()
for user in ctx.message.mentions: for user in ctx.message.mentions:
self.con.serverconfig[ctx.message.server.id]["twitch"]["whitelist"]["list"].append(user.id)
self.con.updateconfig()
self.serverconfig[ctx.message.server.id]["twitch"]["whitelist"]["list"].append(user.id)
self.con.updateconfig(self.serverconfig)
whitelist_count += 1 whitelist_count += 1
return await self.bot.say('{} user(s) have been added to the whitelist'.format(whitelist_count)) return await self.bot.say('{} user(s) have been added to the whitelist'.format(whitelist_count))


elif option in ['-', 'remove']: elif option in ['-', 'remove']:
self.con.serverconfig = self.con.load_config()
self.serverconfig = self.con.load_config()
for user in ctx.message.mentions: for user in ctx.message.mentions:
if user.id in self.con.serverconfig[ctx.message.server.id]["twitch"]["whitelist"]["list"]:
self.con.serverconfig[ctx.message.server.id]["twitch"]["whitelist"]["list"].remove(user.id)
self.con.updateconfig()
if user.id in self.serverconfig[ctx.message.server.id]["twitch"]["whitelist"]["list"]:
self.serverconfig[ctx.message.server.id]["twitch"]["whitelist"]["list"].remove(user.id)
self.con.updateconfig(self.serverconfig)
whitelist_count += 1 whitelist_count += 1
return await self.bot.say('{} user(s) have been removed to the whitelist'.format(whitelist_count)) return await self.bot.say('{} user(s) have been removed to the whitelist'.format(whitelist_count))


elif option == 'list': elif option == 'list':
return await self.bot.say( return await self.bot.say(
self.con.serverconfig[ctx.message.server.id]["twitch"]["whitelist"]["list"])
self.serverconfig[ctx.message.server.id]["twitch"]["whitelist"]["list"])




def setup(bot): def setup(bot):

+ 3
- 2
cogs/__init__.py View File

import json
cogs = [ cogs = [
'cogs.Admin', 'cogs.Admin',
'cogs.Twitch', 'cogs.Twitch',
'cogs.selfAssign', 'cogs.selfAssign',
'cogs.Fun'
'cogs.Fun',
'cogs.joinLeave',
'config.config'
] ]

+ 43
- 0
cogs/joinLeave.py View File

import discord

from config.config import Config

class joinLeave():
def __init__(self, Bot):
self.bot = Bot
self.con = Config(Bot)

async def on_member_join(self, member):
"""
Greets users when they join a server.
"""
# TODO: Move these to a cog cause they suck in main
self.con.load_config()
if not self.con.serverconfig[member.server.id]["greets"]["enabled"]:
return
if self.con.serverconfig[member.server.id]["greets"]["custom-message"]:
message = self.con.serverconfig[member.server.id]["greets"]["custom-message"]
else:
message = self.con.serverconfig[member.server.id]["greets"]["default-message"]
# TODO: Maybe thumbnail for the embed
em = discord.Embed(
title="Welcome to {}!".format(member.server),
description='Hey {}! Welcome to **{}**! {}'.format(member.mention, member.server, message),
colour=0xDEADBF)

if self.con.serverconfig[member.server.id]["greets"]["welcome-channel"]:
channel = discord.Object(self.con.serverconfig[member.server.id]["greets"]["welcome-channel"])
else:
channel = member.server.default_channel
return await self.bot.send_message(channel,embed=em)

async def on_member_remove(self, member):
self.con.load_config()
if not self.con.serverconfig[member.server.id]["goodbyes"]["enabled"]:
return
else:
return await self.bot.send_message(member.server,embed=discord.Embed(
description="{}#{} has left or been beaned.".format(member.name, member.discriminator), colour=0xDEADBF))

def setup(Bot):
Bot.add_cog(joinLeave(Bot))

+ 17
- 17
cogs/selfAssign.py View File

def __init__(self, Bot): def __init__(self, Bot):
self.bot = Bot self.bot = Bot
self.con = Config(Bot) self.con = Config(Bot)
self.serverconfig = self.con.serverconfig


@bot.command(pass_context=True) @bot.command(pass_context=True)
async def listroles(self, ctx): async def listroles(self, ctx):
{command_prefix}listroles {command_prefix}listroles
""" """
roles = [] roles = []
for role in self.con.serverconfig[ctx.message.server.id]["selfAssign"]["roles"]:
for role in self.serverconfig[ctx.message.server.id]["selfAssign"]["roles"]:
for serverrole in ctx.message.server.roles: for serverrole in ctx.message.server.roles:
if role == serverrole.id: if role == serverrole.id:
roles.append(serverrole.name) roles.append(serverrole.name)
return await self.bot.say(str(roles).strip("[]")) return await self.bot.say(str(roles).strip("[]"))



@bot.command(pass_context=True) @bot.command(pass_context=True)
async def iam(self, ctx, role: discord.Role = None): async def iam(self, ctx, role: discord.Role = None):
""" """
Example: Example:
.iam OverwatchPing .iam OverwatchPing
""" """
self.con.serverconfig = self.con.load_config()
if not self.con.serverconfig[ctx.message.server.id]["selfAssign"]["enabled"]:
self.serverconfig = self.con.load_config()
if not self.serverconfig[ctx.message.server.id]["selfAssign"]["enabled"]:
return return


user = ctx.message.author user = ctx.message.author
if role in user.roles: if role in user.roles:
return await self.bot.say("You already have that role.") return await self.bot.say("You already have that role.")


if role.id in self.con.serverconfig[ctx.message.server.id]["selfAssign"]["roles"]:
if role.id in self.serverconfig[ctx.message.server.id]["selfAssign"]["roles"]:
await self.bot.add_roles(user, role) await self.bot.add_roles(user, role)
print("{} added {} to themselves in {} on {}".format(user.display_name, role.name, ctx.message.channel, print("{} added {} to themselves in {} on {}".format(user.display_name, role.name, ctx.message.channel,
ctx.message.server)) ctx.message.server))
Example: Example:
.iamn OverwatchPing .iamn OverwatchPing
""" """
self.con.serverconfig = self.con.load_config()
if not self.con.serverconfig[ctx.message.server.id]["selfAssign"]["enabled"]:
self.serverconfig = self.con.load_config()
if not self.serverconfig[ctx.message.server.id]["selfAssign"]["enabled"]:
return return


user = ctx.message.author user = ctx.message.author
if role not in server.roles: if role not in server.roles:
return await self.bot.say("That role doesn't exist. Roles are case sensitive. ") return await self.bot.say("That role doesn't exist. Roles are case sensitive. ")


elif role in user.roles and role.id in self.con.serverconfig[ctx.message.server.id]["selfAssign"]["roles"]:
elif role in user.roles and role.id in self.serverconfig[ctx.message.server.id]["selfAssign"]["roles"]:
await self.bot.remove_roles(user, role) await self.bot.remove_roles(user, role)
return await self.bot.reply("{} has been successfully removed.".format(role.name)) return await self.bot.reply("{} has been successfully removed.".format(role.name))


elif role not in user.roles and role.id in self.con.serverconfig[ctx.message.server.id]["selfAssign"]["roles"]:
elif role not in user.roles and role.id in self.serverconfig[ctx.message.server.id]["selfAssign"]["roles"]:
return await self.bot.reply("You do not have {}.".format(role.name)) return await self.bot.reply("You do not have {}.".format(role.name))
else: else:
return await self.bot.say("That role is not self-assignable.") return await self.bot.say("That role is not self-assignable.")
if not owner(ctx): if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after) return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
else: else:
if role.id in self.con.serverconfig[ctx.message.server.id]["selfAssign"]["roles"]:
if role.id in self.serverconfig[ctx.message.server.id]["selfAssign"]["roles"]:
return await self.bot.say("{} is already a self-assignable role.".format(role.name), delete_after=self.con.delete_after) return await self.bot.say("{} is already a self-assignable role.".format(role.name), delete_after=self.con.delete_after)
self.con.serverconfig = self.con.load_config()
self.con.serverconfig[ctx.message.server.id]["selfAssign"]["roles"].append(role.id)
self.con.updateconfig()
self.serverconfig = self.con.load_config()
self.serverconfig[ctx.message.server.id]["selfAssign"]["roles"].append(role.id)
self.con.updateconfig(self.serverconfig)
return await self.bot.say('Role "{}" added'.format(str(role))) return await self.bot.say('Role "{}" added'.format(str(role)))


@bot.command(pass_context=True, hidden=True) @bot.command(pass_context=True, hidden=True)
if not owner(ctx): if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after) return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
else: else:
self.con.serverconfig = self.con.load_config()
if role.id in self.con.serverconfig[ctx.message.server.id]["selfAssign"]["roles"]:
self.con.serverconfig[ctx.message.server.id]["selfAssign"]["roles"].remove(role.id)
self.con.updateconfig()
self.serverconfig = self.con.load_config()
if role.id in self.serverconfig[ctx.message.server.id]["selfAssign"]["roles"]:
self.serverconfig[ctx.message.server.id]["selfAssign"]["roles"].remove(role.id)
self.con.updateconfig(self.serverconfig)
return await self.bot.say('"{}" has been removed from the self-assignable roles.'.format(str(role))) return await self.bot.say('"{}" has been removed from the self-assignable roles.'.format(str(role)))
else: else:
return await self.bot.say("That role was not in the list.") return await self.bot.say("That role was not in the list.")

+ 6
- 6
config/config.py View File



async def on_server_join(self, server): async def on_server_join(self, server):
self.serverconfig[server.id] = self.serverconfig_template["example"] self.serverconfig[server.id] = self.serverconfig_template["example"]
self.updateconfig()
self.updateconfig(self.serverconfig)


async def on_server_remove(self, server): async def on_server_remove(self, server):
self.serverconfig.pop(server.id) self.serverconfig.pop(server.id)
self.updateconfig()
self.updateconfig(self.serverconfig)


def load_config(self): def load_config(self):
with open('config/config.json', 'r') as config_file: with open('config/config.json', 'r') as config_file:
return json.load(config_file) return json.load(config_file)


def updateconfig(self):
def updateconfig(self, config):
with open('config/config.json', 'w') as conf_file: with open('config/config.json', 'w') as conf_file:
json.dump(self.serverconfig, conf_file)
json.dump(config, conf_file)


def config_errorcheck(self): def config_errorcheck(self):
# TODO: Fix so that it checks for problems in children of module settings. i.e children of 'greets' # TODO: Fix so that it checks for problems in children of module settings. i.e children of 'greets'
for server in self.bot.servers: for server in self.bot.servers:
if server.id not in self.serverconfig: if server.id not in self.serverconfig:
self.serverconfig[server.id] = self.serverconfig_template["example"] self.serverconfig[server.id] = self.serverconfig_template["example"]
self.updateconfig()
self.updateconfig(self.serverconfig)
print( print(
"WARNING: The config file for {} was not found. A template has been loaded and saved. All modules are turned off by default.".format( "WARNING: The config file for {} was not found. A template has been loaded and saved. All modules are turned off by default.".format(
server.name.upper())) server.name.upper()))
if module_setting not in self.serverconfig[server.id]: if module_setting not in self.serverconfig[server.id]:
self.serverconfig[server.id][module_setting] = self.serverconfig_template["example"][ self.serverconfig[server.id][module_setting] = self.serverconfig_template["example"][
module_setting] module_setting]
self.updateconfig()
self.updateconfig(self.serverconfig)
print( print(
"WARNING: The config file for {} was missing the {} module. This has been fixed with the template version. It is disabled by default.".format( "WARNING: The config file for {} was missing the {} module. This has been fixed with the template version. It is disabled by default.".format(
server.name.upper(), module_setting.upper())) server.name.upper(), module_setting.upper()))

+ 0
- 36
main.py View File

return await bot.process_commands(message) return await bot.process_commands(message)




@bot.event
async def on_member_join(member):
"""
Greets users when they join a server.
"""
# TODO: Move these to a cog cause they suck in main
con.load_config()
if not con.serverconfig[member.server.id]["greets"]["enabled"]:
return
if con.serverconfig[member.server.id]["greets"]["custom-message"]:
message = con.serverconfig[member.server.id]["greets"]["custom-message"]
else:
message = con.serverconfig[member.server.id]["greets"]["default-message"]
# TODO: Maybe thumbnail for the embed
em = discord.Embed(
title="Welcome to {}!".format(member.server),
description='Hey {}! Welcome to **{}**! {}'.format(member.mention, member.server, message),
colour=0xDEADBF)

if con.serverconfig[member.server.id]["greets"]["welcome-channel"]:
channel = discord.Object(con.serverconfig[member.server.id]["greets"]["welcome-channel"])
else:
channel = member.server.default_channel
return await bot.send_message(channel,embed=em)


@bot.event
async def on_member_remove(member):
con.load_config()
if not con.serverconfig[member.server.id]["goodbyes"]["enabled"]:
return
else:
return await bot.send_message(member.server,embed=discord.Embed(
description="{}#{} has left or been beaned.".format(member.name, member.discriminator), colour=0xDEADBF))


if __name__ == "__main__": if __name__ == "__main__":
bot.run(token) bot.run(token)

Loading…
Cancel
Save