This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
160 lines (132 loc) · 6.66 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import dotenv
dotenv.load_dotenv(verbose=True)
import os
import chalk
import discord
from discord.ext import commands
from tinydb import TinyDB, Query, where
import log
db = TinyDB("db.json")
teams_table = db.table("teams")
users_table = db.table("users")
bot = commands.Bot(command_prefix="cf!")
bot.remove_command('help')
# bot.remove_command("help")
token = os.getenv("TOKEN")
@bot.event
async def on_ready():
await bot.change_presence(activity=discord.Game("in CodeFest 2021"))
log.good("Bot is ready!")
@bot.command()
async def help(ctx: commands.Context):
embed = discord.Embed(title="Help Menu", description="`cf!help` — Shows this help message\n`cf!teams` — List teams\n`cf!create_team <team name>` — Create a team\n`cf!remove_team <team name>` — Remove a team\n`cf!join <team name>` — Join a team\n`cf!leave` — Leave your current team", color=0x63e2ff)
await ctx.channel.send(embed=embed)
# TEAM MANAGEMENT
@bot.command()
async def create_team(ctx: commands.Context, *, team_name: str):
if(ctx.channel.id != 817150686114086942):
return
teams = teams_table.search(where('name') == team_name)
if(len(teams) > 0):
await ctx.channel.send(embed=discord.Embed(title=f"This team already exists.", color=0x63e2ff))
return
team_role = await ctx.guild.create_role(name=team_name)
exec_role = discord.utils.get(ctx.guild.roles, id=760588477061398609)
overwrites = {
ctx.guild.default_role: discord.PermissionOverwrite(read_messages=False, send_messages=False),
ctx.guild.me: discord.PermissionOverwrite(read_messages=True, send_messages=True),
team_role: discord.PermissionOverwrite(read_messages=True, send_messages=True),
exec_role: discord.PermissionOverwrite(read_messages=True, send_messages=True),
}
teams_category = discord.utils.get(ctx.guild.categories, id=int(os.getenv("TEAM_CATEGORY")))
text_channel = await teams_category.create_text_channel(team_name, overwrites=overwrites)
voice_channel = await teams_category.create_voice_channel(team_name, overwrites=overwrites)
teams_table.insert({'name': team_name, 'owner': ctx.author.id, 'text_channel': text_channel.id, 'voice_channel': voice_channel.id})
await ctx.channel.send(embed=discord.Embed(title=f"Team **{team_name}** has been created.", color=0x63e2ff))
log.good(f"Team \"{team_name}\" has been created.")
@bot.command()
async def teams(ctx: commands.Context):
teams = teams_table.all()
embed = discord.Embed(title="Team List", color=0x63e2ff)
for team in teams:
members = users_table.search(where('team') == team['name'])
members_string = ""
try:
for member in members:
username = bot.get_user(member['id']).name
members_string = members_string + username + ", "
except:
members_string = "Users temporarily disabled"
if len(members_string) == 0:
members_string = "No members."
embed.add_field(name=team['name'], value=members_string)
await ctx.channel.send(embed=embed)
log.good("Listed teams.")
@bot.command()
@commands.has_role("Executives")
async def force_remove_team(ctx: commands.Context, *, team_name: str):
teams = teams_table.search(where('name') == team_name)
for team in teams:
await discord.utils.get(ctx.guild.text_channels, id=team['text_channel']).delete()
await discord.utils.get(ctx.guild.voice_channels, id=team['voice_channel']).delete()
role = discord.utils.get(ctx.guild.roles, name=team_name)
await role.delete()
teams_table.remove(where('name') == team_name)
await ctx.channel.send(embed=discord.Embed(title=f"Team **{team_name}** has been removed.", color=0x63e2ff))
log.good(f"Team \"{team_name}\" has been removed.")
@bot.command()
async def remove_team(ctx: commands.Context, *, team_name: str):
if(ctx.channel.id != 817150686114086942):
return
teams = teams_table.search(where('name') == team_name)
for team in teams:
if team['owner'] != ctx.author.id:
await ctx.channel.send(embed=discord.Embed(title=f"You do not own **{team_name}**.", color=0x63e2ff))
return
else:
await discord.utils.get(ctx.guild.text_channels, id=team['text_channel']).delete()
await discord.utils.get(ctx.guild.voice_channels, id=team['voice_channel']).delete()
role = discord.utils.get(ctx.guild.roles, name=team_name)
await role.delete()
teams_table.remove(where('name') == team_name)
await ctx.channel.send(embed=discord.Embed(title=f"Team **{team_name}** has been removed.", color=0x63e2ff))
log.good(f"Team \"{team_name}\" has been removed.")
# USER MANAGEMENT
@bot.command()
async def join(ctx: commands.Context, *, team_name: str):
if(ctx.channel.id != 817150686114086942):
return
teams = teams_table.search(where('name') == team_name)
users = users_table.search(where('id') == ctx.author.id)
if(len(teams) == 0):
await ctx.channel.send(embed=discord.Embed(title=f"Team **{team_name}** was not found.", color=0x63e2ff))
return
if(len(users) == 0):
users_table.insert({'id': ctx.author.id, 'team': team_name})
role = discord.utils.get(ctx.guild.roles, name=team_name)
await ctx.author.add_roles(role)
await ctx.channel.send(embed=discord.Embed(title=f"You have joined **{team_name}**.", color=0x63e2ff))
else:
users[0].update({'team': team_name})
await ctx.channel.send(embed=discord.Embed(title=f"You have joined **{team_name}**.", color=0x63e2ff))
@bot.command()
async def team(ctx: commands.Context):
users = users_table.search(where('id') == ctx.author.id)
if(len(users) == 0):
await ctx.channel.send(embed=discord.Embed(title=f"You are not in a team.", color=0x63e2ff))
else:
await ctx.channel.send(embed=discord.Embed(title=f"You are in **{users[0]['team']}**.", color=0x63e2ff))
@bot.command()
async def leave(ctx: commands.Context):
if(ctx.channel.id != 817150686114086942):
return
users = users_table.search(where('id') == ctx.author.id)
if(len(users) == 0):
await ctx.channel.send(embed=discord.Embed(title=f"You are not in a team.", color=0x63e2ff))
else:
team_name = users_table.search(where('id') == ctx.author.id)[0]['team']
users_table.remove(where('id') == ctx.author.id)
role = discord.utils.get(ctx.guild.roles, name=team_name)
await ctx.author.remove_roles(role)
await ctx.channel.send(embed=discord.Embed(title=f"You are no longer in **{users[0]['team']}**.", color=0x63e2ff))
bot.run(token)