-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
192 lines (151 loc) · 6.66 KB
/
main.py
File metadata and controls
192 lines (151 loc) · 6.66 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import discord
import discord.app_commands as commands
import json
def load():
with open(file='./config/config.json', mode='r', encoding='utf-8') as file:
return json.loads(file.read())
def save(config):
with open(file='./config/config.json', mode='w', encoding='utf-8') as file:
file.write(json.dumps(config, indent=4))
config = load()
print(config)
token = config['token']
intents = discord.Intents.all()
bot = discord.Client(intents=intents)
tree = commands.CommandTree(bot)
# コマンド実装
@tree.command(name="edit_join_message", description="メンバーが参加したときに送信するメッセージを編集します")
@commands.describe(title="参加時に送信するembedのタイトル")
@commands.describe(text="参加時に送信するembedの本文")
async def edit_join_message(interaction: discord.Interaction, title: str, text: str):
await interaction.response.defer()
for role in interaction.user.roles:
if role.permissions.administrator:
# 処理を書く
config['Wisdom']['message']['join_message']['title'] = title
config['Wisdom']['message']['join_message']['description'] = text
save(config)
embed = discord.Embed(
title="Done",
description="### メッセージの編集に成功しました\n反映には時間がかかる可能性がございます\n### 内容\nTitle: {}\nText: {}".format(title, text),
color=discord.Color.green()
)
await interaction.followup.send(embed=embed)
return
embed = discord.Embed(
title="Error",
description="You don't have permission to edit this message",
color=discord.Color.red()
)
await interaction.followup.send(embed=embed)
return
@tree.command(name="edit_left_message", description="メンバーが脱退したときに送信するメッセージを編集します")
@commands.describe(title="脱退時に送信するembedのタイトル")
@commands.describe(text="脱退時に送信するembedの本文")
async def edit_left_message(interaction: discord.Interaction, title: str, text: str):
await interaction.response.defer()
for role in interaction.user.roles:
if role.permissions.administrator:
# 処理を書く
config['Wisdom']['message']['left_message']['title'] = title
config['Wisdom']['message']['left_message']['description'] = text
save(config)
embed = discord.Embed(
title="Done",
description="### メッセージの編集に成功しました\n反映には時間がかかる可能性がございます\n### 内容\nTitle: {}\nText: {}".format(
title, text),
color=discord.Color.green()
)
await interaction.followup.send(embed=embed)
return
embed = discord.Embed(
title="Error",
description="You don't have permission to edit this message",
color=discord.Color.red()
)
await interaction.followup.send(embed=embed)
return
@tree.command(name="change_join_message_channel", description="参加時にメッセージを送信するチャンネルを変更します")
@commands.describe(channel="メッセージを送信するチャンネル")
async def change_join_message_channel(interaction: discord.Interaction, channel: discord.TextChannel):
await interaction.response.defer()
for role in interaction.user.roles:
if role.permissions.administrator:
config['Wisdom']['join_action_channel'] = channel.id
save(config)
embed = discord.Embed(
title="Done",
description="# 送信先チャンネルを変更しました\n{}".format(channel.jump_url),
color=discord.Color.green()
)
await interaction.followup.send(embed=embed)
return
embed = discord.Embed(
title="Error",
description="You don't have permission to change",
color=discord.Color.red()
)
await interaction.followup.send(embed=embed)
return
# コマンドはこの前に
@tree.command(name="reload", description="botのコマンドを再読み込みします")
async def reload(interaction: discord.Interaction):
await interaction.response.defer()
for role in interaction.user.roles:
if role.permissions.administrator:
await tree.sync()
embed = discord.Embed(
title="Done Reload",
description="コマンドの再読み込みに成功しました\n実装には時間がかかる可能性があります",
color=discord.Color.green()
)
await interaction.followup.send(embed=embed)
return
embed = discord.Embed(
title="Error",
description="You don't have permission to reload",
color=discord.Color.red()
)
await interaction.followup.send(embed=embed)
return
# ここからメッセージとかもろもろの処理
@bot.event
async def on_ready():
print('Logged in as ' + bot.user.name)
await tree.sync()
return
@bot.event
async def on_member_join(member: discord.Member):
join_action_channel: int = int(config['Wisdom']['join_action_channel'])
action_message = config['Wisdom']['message']
embed = discord.Embed(
title=action_message['join_message']['title'],
description=action_message['join_message']['description'].replace('[user]', '<@{}>'.format(member.id)),
color=discord.Color.green()
)
await bot.get_channel(join_action_channel).send(embed=embed)
if member.bot:
role_id: int = int(config['Wisdom']['bot_role'])
role = member.guild.get_role(role_id)
await member.add_roles(role)
log_channel_id: int = int(action_message['other_log_channel'])
log_channel = bot.get_channel(log_channel_id)
embed = discord.Embed(
title="Add role",
description="<@{}>に<@&{}>を付与しました".format(member.id, role_id),
color=discord.Color.green()
)
await log_channel.send(embed=embed)
return
@bot.event
async def on_member_remove(member: discord.Member):
left_action_channel: int = int(config['Wisdom']['left_action_channel'])
action_message = config['Wisdom']['message']
embed = discord.Embed(
title=action_message['left_message']['title'],
description=action_message['left_message']['description'].replace('[user]', '<@{}>'.format(member.id)),
color=discord.Color.red()
)
await bot.get_channel(left_action_channel).send(embed=embed)
return
bot.run(token)