1
1
import argparse
2
2
import csv
3
+ from itertools import chain
3
4
from os import getenv
4
5
6
+ from discord import PermissionOverwrite , Permissions
5
7
from discord .ext import commands
6
8
7
9
bot = commands .Bot (command_prefix = "/" )
@@ -38,6 +40,40 @@ async def edit_channels_topic(guild, csv_path):
38
40
await edit_topic (guild , int (row ["channel_id" ]), message )
39
41
40
42
43
+ async def sync_channel_permissions_with_category (guild ):
44
+ for category_channel in guild .categories :
45
+ for channel in chain (
46
+ category_channel .text_channels , category_channel .voice_channels
47
+ ):
48
+ # カテゴリに属すチャンネルで、権限がカテゴリと同期していなければ同期する
49
+ if channel .category and not channel .permissions_synced :
50
+ print (channel .category .name , channel .name )
51
+ # TODO: 必須ではないが、Missing Access (discord.errors.Forbidden) 対応
52
+ await channel .edit (sync_permissions = True )
53
+
54
+
55
+ def create_read_only_overwrite ():
56
+ allow_permissions = Permissions (
57
+ read_messages = True , read_message_history = True
58
+ )
59
+ deny_permissions = Permissions .all ()
60
+ deny_permissions .read_messages = False
61
+ deny_permissions .read_message_history = False
62
+ overwrite = PermissionOverwrite .from_pair (
63
+ allow_permissions , deny_permissions
64
+ )
65
+ return overwrite
66
+
67
+
68
+ async def set_categories_read_only (guild , role_name ):
69
+ # カテゴリの@everyoneの権限の対応で済むことが分かり、実装中のこの関数は使わなくなった
70
+ # TODO: @everyoneの権限の編集の自動化の余地あり(BotのOAuthがいるかも)
71
+ role = [role for role in guild .roles if role .name == role_name ][0 ]
72
+ read_only = create_read_only_overwrite ()
73
+ for category_channel in guild .categories [:2 ]:
74
+ await category_channel .set_permissions (role , overwrite = read_only )
75
+
76
+
41
77
@bot .event
42
78
async def on_ready ():
43
79
print ("ready!" )
@@ -50,6 +86,14 @@ async def on_ready():
50
86
if args .subcommand == "edit_channels_topic" :
51
87
await edit_channels_topic (guild , args .input_csv )
52
88
89
+ if args .subcommand == "archive" :
90
+ if args .archive_command == "sync_channel_permissions" :
91
+ await sync_channel_permissions_with_category (guild )
92
+ if args .archive_command == "set_read_only" :
93
+ await set_categories_read_only (guild , args .role_name )
94
+
95
+ print ("Command completed!" )
96
+
53
97
54
98
parser = argparse .ArgumentParser ()
55
99
subparsers = parser .add_subparsers (dest = "subcommand" )
@@ -60,6 +104,14 @@ async def on_ready():
60
104
edit_channels_topic_parser = subparsers .add_parser ("edit_channels_topic" )
61
105
edit_channels_topic_parser .add_argument ("input_csv" )
62
106
107
+ archive_parser = subparsers .add_parser ("archive" )
108
+ archive_subparsers = archive_parser .add_subparsers (dest = "archive_command" )
109
+ sync_channel_permissions_parser = archive_subparsers .add_parser (
110
+ "sync_channel_permissions"
111
+ )
112
+ set_read_only_parser = archive_subparsers .add_parser ("set_read_only" )
113
+ set_read_only_parser .add_argument ("role_name" )
114
+
63
115
args = parser .parse_args ()
64
116
65
117
token = getenv ("DISCORD_BOT_TOKEN" )
0 commit comments