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

@@ -18,6 +18,7 @@ class Admin():
def __init__(self, Bot):
self.bot = Bot
self.con = Config(Bot)
self.serverconfig = self.con.serverconfig

@bot.command(pass_context=True, hidden=True)
async def blacklist(self, ctx, option, *args):
@@ -80,27 +81,30 @@ class Admin():
if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
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))
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))
else:
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)
async def set_welcomechannel(self, ctx, channel: discord.Channel = None):
if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
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))

@bot.command(pass_context=True, hidden=True)
@@ -108,9 +112,9 @@ class Admin():
if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
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))

@bot.command(pass_context=True, hidden=True)
@@ -118,9 +122,9 @@ class Admin():
if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
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))

@bot.command(pass_context=True, hidden=True)
@@ -128,9 +132,9 @@ class Admin():
if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
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))

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

+ 19
- 18
cogs/Twitch.py View File

@@ -9,6 +9,7 @@ class Twitch():
def __init__(self, bot):
self.bot = bot
self.con = Config(bot)
self.serverconfig = self.con.serverconfig

async def on_member_update(self, member_b, member_a):
# Twitch Shilling Part
@@ -16,12 +17,12 @@ class Twitch():
return
if member_a.game:
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 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,
content=":video_game:** {} is live!** :video_game:\n{}\n{}".format(
member_a.name, member_a.game.name, member_a.game.url))
@@ -31,14 +32,14 @@ class Twitch():
if not owner(ctx):
return await self.bot.reply("You do not have permission to do this command.", delete_after=20)
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.")
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.")

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

if option in ['+', 'add']:
self.con.serverconfig = self.con.load_config()
self.serverconfig = self.con.load_config()
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
return await self.bot.say('{} user(s) have been added to the whitelist'.format(whitelist_count))

elif option in ['-', 'remove']:
self.con.serverconfig = self.con.load_config()
self.serverconfig = self.con.load_config()
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
return await self.bot.say('{} user(s) have been removed to the whitelist'.format(whitelist_count))

elif option == 'list':
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):

+ 3
- 2
cogs/__init__.py View File

@@ -1,7 +1,8 @@
import json
cogs = [
'cogs.Admin',
'cogs.Twitch',
'cogs.selfAssign',
'cogs.Fun'
'cogs.Fun',
'cogs.joinLeave',
'config.config'
]

+ 43
- 0
cogs/joinLeave.py View File

@@ -0,0 +1,43 @@
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

@@ -13,6 +13,7 @@ class selfAssign():
def __init__(self, Bot):
self.bot = Bot
self.con = Config(Bot)
self.serverconfig = self.con.serverconfig

@bot.command(pass_context=True)
async def listroles(self, ctx):
@@ -22,13 +23,12 @@ class selfAssign():
{command_prefix}listroles
"""
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:
if role == serverrole.id:
roles.append(serverrole.name)
return await self.bot.say(str(roles).strip("[]"))


@bot.command(pass_context=True)
async def iam(self, ctx, role: discord.Role = None):
"""
@@ -38,8 +38,8 @@ class selfAssign():
Example:
.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

user = ctx.message.author
@@ -51,7 +51,7 @@ class selfAssign():
if role in user.roles:
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)
print("{} added {} to themselves in {} on {}".format(user.display_name, role.name, ctx.message.channel,
ctx.message.server))
@@ -68,8 +68,8 @@ class selfAssign():
Example:
.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

user = ctx.message.author
@@ -78,11 +78,11 @@ class selfAssign():
if role not in server.roles:
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)
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))
else:
return await self.bot.say("That role is not self-assignable.")
@@ -92,11 +92,11 @@ class selfAssign():
if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
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)
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)))

@bot.command(pass_context=True, hidden=True)
@@ -104,10 +104,10 @@ class selfAssign():
if not owner(ctx):
return await self.bot.reply(self.con.no_perms_reponse, delete_after=self.con.delete_after)
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)))
else:
return await self.bot.say("That role was not in the list.")

+ 6
- 6
config/config.py View File

@@ -38,19 +38,19 @@ class Config():

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

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

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

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

def config_errorcheck(self):
# TODO: Fix so that it checks for problems in children of module settings. i.e children of 'greets'
@@ -58,7 +58,7 @@ class Config():
for server in self.bot.servers:
if server.id not in self.serverconfig:
self.serverconfig[server.id] = self.serverconfig_template["example"]
self.updateconfig()
self.updateconfig(self.serverconfig)
print(
"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()))
@@ -67,7 +67,7 @@ class Config():
if module_setting not in self.serverconfig[server.id]:
self.serverconfig[server.id][module_setting] = self.serverconfig_template["example"][
module_setting]
self.updateconfig()
self.updateconfig(self.serverconfig)
print(
"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()))

+ 0
- 36
main.py View File

@@ -55,41 +55,5 @@ async def on_message(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__":
bot.run(token)

Loading…
Cancel
Save