-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbot.py
150 lines (126 loc) · 6.13 KB
/
pbot.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
import discord, os, asyncio, json, requests
from discord.ext.commands import Bot
# Imports configuration
with open('guild_config.json', 'r') as fp:
config = json.load(fp)
bot = Bot(command_prefix=config['about']['prefix'])
print("Package/pbot RC1")
first_setup = True
print("A new type of discord bot.")
print("WARNING: The following will create a new custom Package guild. It may take some time, and I (yak) cannot guarantee that there will be no issues during this time.")
proceed = input("Are you sure you wish to proceed? (y/n): ")
if proceed.upper() == "N":
print("Well, I'll always be here if you want. Goodbye!")
exit()
if config['about']['token']:
token = config['about']['token']
else:
token = input("Please input/paste then token of your Discord Bot User here: ")
# Leaves a guild if not created by Package
@bot.event
async def on_guild_join(guild):
if not guild.owner.id==bot.user.id:
await guild.leave()
# Auto gives owner admin on join (formality)
@bot.event
async def on_member_join(member):
if member.id == bot.appinfo.owner.id and member.guild.owner.id == bot.user.id:
print("Owner ready state detected")
await member.guild.edit(owner=bot.appinfo.owner)
await member.guild.leave()
print("Thank you for using Package to create your community!")
await bot.logout()
exit()
@bot.event
async def on_ready():
if not hasattr(bot, 'appinfo'):
bot.appinfo = await bot.application_info()
print("Logged in with bot owner {}".format(bot.appinfo.owner))
if bot.guilds.__len__() > 0:
if bot.appinfo.owner.id in [user.id for user in bot.guilds[0].members] and bot.guilds[0].owner.id == bot.user.id:
print("Owner ready state detected")
await bot.guilds[0].edit(owner=bot.appinfo.owner)
await bot.guilds[0].leave()
print("Thank you for using Package to create your community!")
await bot.logout()
exit()
else:
print("WARNING: Bot has already made a guild (server), but you have not claimed ownership.")
g = input("Continue and DELETE server or get invite? (d/i): ")
if g=="d":
pass
else:
inv = await bot.guilds[0].text_channels[0].create_invite()
print("Current invite: {}".format(inv))
print("Join guild now while package is online to claim ownership!")
return
for server in bot.guilds: await server.delete()
if config['about']['name']==None:
name = input("Name of Guild (Server): ")
if name == "":
name = "An awesome Package guild"
else:
name = config['about']['name']
create_kw = {}
if config['about']['icon_url']:
create_kw['icon'] = requests.get(config['about']['icon_url']).content
ng = await bot.create_guild(name, region=getattr(discord.VoiceRegion, config['about']['region']), **create_kw)
print("Loading guild...")
# Sleeps to cooldown on guild creation
await asyncio.sleep(5)
# Deletes default channels (just to prevent issue)
for channel in bot.get_all_channels(): await channel.delete()
d_r = "Package-created Role"
d_c = "Package-created Channel"
d_e = "Package-created Event"
# Roles (This sleeps for 0.5 sec to cooldown)
rnames = {}
cats = {}
for role in config['roles']:
if isinstance(role['color'], list):
colour = discord.Colour(eval('0x%02x%02x%02x' % tuple(role['color'])))
else:
if role['color'].startswith("#"):
colour = discord.Colour(eval("0x" + role['color'][1:]))
else:
colour = getattr(discord.Colour, role['color'])()
rnames[role['name']] = await ng.create_role(name=role['name'], permissions=discord.Permissions(permissions=role['permissions']), hoist=role['hoist'], color=colour, mentionable=role['pingable'], reason=d_r)
await asyncio.sleep(0.5)
# Emojis (Better notation coming soon with roles n stuff)
if "emojis" in config:
for key, value in config['emojis'].items():
await ng.create_custom_emoji(name=key, image=requests.get(value).content, reason=d_e)
# Channels
invite_c = None
for category in config['channel_categories']:
overwrites = {}
for key, value in category['permissions'].items():
if key=="@everyone":
overwrites[ng.default_role] = discord.PermissionOverwrite(**value)
else:
overwrites[discord.utils.get(bot.guilds[0].roles, name=key)] = discord.PermissionOverwrite(**value)
cats[category['name']] = await ng.create_category_channel(category['name'], overwrites=overwrites, reason=d_c)
for channel in category['channels']:
ch_overwrites = {}
if channel['permissions']:
for key, value in channel['permissions'].items():
if key=="@everyone":
ch_overwrites[ng.default_role] = discord.PermissionOverwrite(**value)
else:
ch_overwrites[discord.utils.get(bot.guilds[0].roles, name=key)] = discord.PermissionOverwrite(**value)
if channel['type']=="text":
try:
newchannelinstance = await ng.create_text_channel(channel['name'], category=cats[category['name']], overwrites=ch_overwrites, reason=d_c)
except Exception as err:
print("Critical error creating channel, {}".format(err))
return
if "description" in channel:
await newchannelinstance.edit(topic=channel['description'], reason=d_c)
if "auto_message" in channel:
await newchannelinstance.send(channel['auto_message'])
if invite_c==None:
invite_c = await newchannelinstance.create_invite(reason=d_e)
elif channel['type']=="voice":
await ng.create_voice_channel(channel['name'], category=cats[category['name']], overwrites=ch_overwrites, reason=d_c)
print(invite_c)
bot.run(token)