-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchanmod.py
151 lines (142 loc) · 5.93 KB
/
chanmod.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
# chanmod.py
import discord
import time
async def makenewchannel(theguild, channelname, requser: discord.Member):
# use time as a counter to prevent name clashes
newname = ""
makepublic = True
startpos = -1
if channelname.startswith('public_channel_'): # String pre-formatted a bit, spaces are underscores and all valid characters
startpos=15
elif channelname.startswith('private_channel_'):
startpos=16
makepublic = False
elif channelname.startswith('channel_'): # public/private not specified. make public
startpos=8
else:
return "**invalid format, get !help**"
if len(channelname)<startpos+1:
return "**no channel name specified**"
newname = channelname[startpos:]
uniqkey = int(time.time())
newrolename = requser.name + "-" + newname + "-" + str(uniqkey)
if makepublic==False:
# new role to make channel private, restrict read/write
try:
await theguild.create_role(name=newrolename,mentionable=False)
newrole = discord.utils.get(theguild.roles, name=newrolename)
await requser.add_roles(newrole,reason="private channel",atomic=True)
except BaseException as e:
return str(e)
noaccess = discord.PermissionOverwrite()
noaccess.send_messages = False
noaccess.read_messages = False
noaccess.view_channel = False
noaccess.stream = False
noaccess.embed_links = False
noaccess.attach_files = False
noaccess.read_message_history = False
noaccess.mention_everyone = False
yesaccess = discord.PermissionOverwrite()
yesaccess.send_messages = True
yesaccess.read_messages = True
yesaccess.view_channel = True
yesaccess.stream = True
yesaccess.embed_links = True
yesaccess.attach_files = True
yesaccess.read_message_history = True
specrules = { newrole: yesaccess, theguild.default_role: noaccess }
try:
await theguild.create_text_channel(name=newrolename,overwrites=specrules,reason=requser.name+" private channel")
except BaseException as e:
return str(e)
else:
try:
await theguild.create_text_channel(name=newrolename,reason=requser.name+" public channel")
except BaseException as e:
return str(e)
return channelname
async def deleteuserchannel(theguild, channelname, requser):
fullchannelname = ""
for chan in theguild.channels:
if chan.name.startswith(requser.name):
if chan.name[len(requser.name)+1:len(requser.name)+len(channelname)+1]==channelname:
try:
await chan.delete(reason="Deleting user channel")
except BaseException as e:
return str(e)
fullchannelname = chan.name
break
if fullchannelname=="":
return "**Couldn\'t find your channel** \"" + channelname + "\""
rolelist = await theguild.fetch_roles()
for arole in rolelist:
if arole.name==fullchannelname:
try:
await arole.delete(reason="Deleting role for private channel")
except BaseException as e:
return str(e)
break
return channelname
async def updateuserchannelrole(theguild, userchan, requser, userop):
addinguser = False
if userop=="add":
addinguser=True
elif userop!="delete":
print(f"chanmod.updateuserchannelrole called with userop=\"{userop}\"\n")
theusername = ""
thechannel = ""
donewithuser = False
for c in userchan:
if c=='_' and theguild.get_member_named(theusername)!=None:
donewithuser=True
elif donewithuser==False:
theusername+=c
else:
thechannel+=c
if donewithuser==False:
return "failure", "Couldn\'t find the user in \"" + userchan + "\""
if thechannel=="":
return "failure", "No channel to add \"" + theusername + "\" to"
fullchannelname = ""
for chan in theguild.channels:
if chan.name.startswith(requser.name):
if chan.name[len(requser.name)+1:len(requser.name)+len(thechannel)+1]==thechannel:
fullchannelname = chan.name
break
if fullchannelname=="":
return "failure", "Couldn\'t find your channel \"" + thechannel + "\""
otheruser = theguild.get_member_named(theusername)
if otheruser is None:
return "failure", "Couldn\'t find the user " + theusername
existingrole = discord.utils.get(theguild.roles, name=fullchannelname)
try:
if addinguser==True:
await otheruser.add_roles(existingrole,reason="added to private channel",atomic=True)
else:
await otheruser.remove_roles(existingrole,reason="removed from private channel",atomic=True)
except BaseException as e:
return "failure", str(e)
return "success", theusername
'''
discord.abc.Snowflake { .id,.created_at } # abstract, almost all Discord models are this
discord.abc.GuildChannel { .name,.guild
discord.Role {
id=(int) id for this role, name=(str),mentionable=False
coroutine .delete(*,reason=None) # deletes the role
discord.Member() {
.display_name
coroutine .remove_roles(roles=[abc.Snowlake,*],reason="private channel",atomic=True)
coroutine .add_roles(roles=[abc.Snowlake,*],reason="private channel",atomic=True) # raises Forbidden / HTTP
overwritesdict = { (Member or Role) : PermissionOverwrite , * }
discord.Guild() {
.channels = [abc.GuildChannel, TextChannel, VoiceChannel, ...]
.me # like Client.user
.text_channels = [TextChannel,...]
.default_role
.get_member_named(name) # returns discord.Member
coroutine create_text_channel(name=,overwrites=dict,reason=)
discord.TextChannel { (discord.VoiceChannel)
coroutine.delete(*,reason=None) # deletes the channel
coroutine.set_permissions(target=discord.Role,read_messages=False,send_messages=False,reason=)
'''