This repository has been archived by the owner on Mar 28, 2021. It is now read-only.
/
giveme.py
142 lines (128 loc) · 6.77 KB
/
giveme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import discord
from discord.ext import commands
from cogs.utils.dataIO import dataIO
import os
import json
from .utils import checks
class GiveMe:
"""Makes members able to give themselves roles."""
def __init__(self, bot):
self.bot = bot
self.settings = dataIO.load_json("data/giveme/settings.json")
@commands.group(pass_context=True, invoke_without_command=True, no_pm=True)
async def giveme(self, ctx, *, role):
"""Give yourself roles."""
if ctx.message.server.id not in self.settings:
await self.bot.say("There are no giveme's for this server.")
return
elif role not in self.settings[ctx.message.server.id]['givemes']:
await self.bot.say("You can't assign that role to yourself. Yet...")
else:
role = discord.utils.get(ctx.message.server.roles, id=self.settings[ctx.message.server.id]['givemes'][role])
await self.bot.add_roles(ctx.message.author, role)
await self.bot.say("The role has been added, you're welcome.")
@giveme.command(pass_context=True, no_pm=True)
async def list(self, ctx):
"""Lists the giveme's this server has."""
if ctx.message.server.id not in self.settings:
await self.bot.say("This server has no givemes.")
return
elif self.settings[ctx.message.server.id]['givemes'] == {}:
await self.bot.say("This server has no givemes.")
return
else:
givemes = []
for giveme in range(len(self.settings[ctx.message.server.id]['givemes'].keys())):
if giveme != (len(self.settings[ctx.message.server.id]['givemes'].keys()) - 1):
givemes.append(list(self.settings[ctx.message.server.id]['givemes'].keys())[giveme])
await self.bot.say("This server has the following giveme's:\n{} and {}.".format(", ".join(givemes), list(self.settings[ctx.message.server.id]['givemes'].keys())[len(self.settings[ctx.message.server.id]['givemes'].keys()) - 1]))
return
@giveme.command(pass_context=True, no_pm=True)
@checks.admin_or_permissions()
async def add(self, ctx, name, *, role:discord.Role):
"""Adds a role to the list of giveme's, if the name contains spaces put it in quotes (").
Example:
[p]giveme add "role name" role_mention OR
[p]giveme add "role name" name of the role"""
if not ctx.message.server.me.permissions_in(ctx.message.channel).manage_roles:
await self.bot.say("I do not have the manage roles permission here, I cannot assign roles to people untill I do.")
else:
if ctx.message.server.id not in self.settings:
self.settings[ctx.message.server.id] = {'givemes': {}}
self.settings[ctx.message.server.id]['givemes'][name] = role.id
self.save_settings()
await self.bot.say("Giveme has been added.")
@giveme.command(pass_context=True, no_pm=True)
@checks.admin_or_permissions()
async def remove(self, ctx, role):
"""Removes a role from the list of giveme's."""
if ctx.message.server.id not in self.settings:
await self.bot.say("This server has no giveme's.")
return
elif role not in list(self.settings[ctx.message.server.id]['givemes'].keys()):
await self.bot.say("That is not a valid giveme.")
return
else:
del self.settings[ctx.message.server.id]['givemes'][role]
self.save_settings()
await self.bot.say("Giveme has been removed.")
@giveme.command(pass_context=True, no_pm=True)
async def getoff(self, ctx, role):
"""Removes a giveme from you, by name which should be defined in [p]giveme list."""
if ctx.message.server.id not in self.settings:
await self.bot.say("This server has no giveme's I can remove.")
elif role not in list(self.settings[ctx.message.server.id]['givemes'].keys()):
await self.bot.say("That's not a valid giveme.")
else:
try:
if not ctx.message.server.me.permissions_in(ctx.message.channel).manage_roles:
await self.bot.say("I do not have the manage roles permission here, I cannot remove roles from you untill I do.")
else:
role = discord.utils.get(ctx.message.server.roles, id=self.settings[ctx.message.server.id]['givemes'][role])
await self.bot.remove_roles(ctx.message.author, role)
await self.bot.say("Role removed.")
except Exception as e:
await self.bot.say("An error occured while remove the role from you ({}).".format(e))
@giveme.command(pass_context=True, no_pm=True)
@checks.admin_or_permissions()
async def massadd(self, ctx, *, roles):
"""Massadd roles.
example:
[p]giveme massadd dank role, another dank role, another one, another one
These have to be the names of valid roles, they will be checked *insert eyes emoji here*."""
try:
if ", " in roles:
roles = roles.split(", ")
elif "," in roles:
roles = role.split(",")
except:
await self.bot.say("An error occured while splitting your roles.")
return
for role in roles:
roleObj = discord.utils.get(ctx.message.server.roles, name=role)
if roleObj == None:
await self.bot.say("{} is not a valid role.".format(role))
else:
if ctx.message.server.id not in self.settings:
self.settings[ctx.message.server.id] = {'givemes': {role: roleObj.id}}
elif role in self.settings[ctx.message.server.id]['givemes']:
await self.bot.say("Giveme {} is already in the list of giveme's.".format(role))
return
else:
self.settings[ctx.message.server.id]['givemes'][role] = roleObj.id
self.save_settings()
await self.bot.say("Giveme's added.")
def save_settings(self):
dataIO.save_json("data/giveme/settings.json", self.settings)
def check_folders():
if not os.path.exists("data/giveme"):
print("Creating data/giveme folder...")
os.makedirs("data/giveme")
def check_files():
if not os.path.exists("data/giveme/settings.json"):
print("Creating data/giveme/settings.json file...")
dataIO.save_json("data/giveme/settings.json", {})
def setup(bot):
check_folders()
check_files()
bot.add_cog(GiveMe(bot))