-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
183 lines (152 loc) · 7.36 KB
/
Copy pathmain.py
File metadata and controls
183 lines (152 loc) · 7.36 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
import os
import discord
from discord.ext import commands, tasks
import dotenv
import json
dotenv.load_dotenv()
intents = discord.Intents.none()
intents.guilds = True
intents.dm_messages = True
bot = commands.Bot(command_prefix="! ", intents=intents, help_command=None)
button_messages = {}
mute_members = []
try:
with open("mute.json", mode="r") as mute_data:
mute_members = json.load(mute_data)
print("セーブをロードしました。")
except:
print("セーブがありません。")
class SendModal(discord.ui.Modal):
def __init__(self):
super().__init__(title="送信する", timeout=None)
self.content = discord.ui.TextInput(
label="メッセージ内容を入力してください。",
style=discord.TextStyle.long,
required=True,
max_length=1800,
)
self.add_item(self.content)
async def on_submit(self, interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
thread_name = interaction.channel.name
try:
user_id = thread_name.split("(")[1].removesuffix(")")
user = await interaction.client.fetch_user(int(user_id))
except Exception:
await interaction.followup.send(content="チャンネル名からユーザーIDを解析できませんでした。", ephemeral=True)
return
if not user:
await interaction.followup.send(content=f"そのメンバーが存在しません。\nユーザーid: {user_id}", ephemeral=True)
return
try:
await user.send(content=self.content.value + f"\n-# {interaction.user.name}が担当しています。")
except discord.Forbidden:
await interaction.followup.send(content="ユーザーがDMを閉じてお出かけ中か、ブロックされているため送信できませんでした。", ephemeral=True)
return
channel = bot.get_channel(1523887748287692850)
if channel:
webhooks = await channel.webhooks()
webhook = discord.utils.get(webhooks, name="ModMail")
if webhook:
await webhook.send(
content=self.content.value,
allowed_mentions=discord.AllowedMentions.none(),
thread=interaction.channel,
avatar_url=interaction.user.display_avatar.url,
username=interaction.user.name
)
button_message = button_messages.get(thread_name)
if button_message:
try:
await button_message.delete()
except discord.NotFound:
pass
view = discord.ui.View(timeout=None)
view.add_item(discord.ui.Button(label="送信", custom_id="reply_send", style=discord.ButtonStyle.primary))
view.add_item(discord.ui.Button(label="ミュート", custom_id="mute", style=discord.ButtonStyle.red))
button_messages[thread_name] = await interaction.channel.send(view=view)
@tasks.loop(seconds=10)
async def status_loop():
await bot.change_presence(activity=discord.CustomActivity(name="連絡はDMへ", emoji="📩"))
with open("mute.json", mode="w") as mute_data:
mute_data.write(json.dumps(mute_members))
@bot.event
async def on_ready():
status_loop.start()
print('Ready.')
@bot.event
async def on_interaction(interaction: discord.Interaction):
if interaction.type == discord.InteractionType.component:
if interaction.data.get("component_type") == 2:
custom_id = interaction.data.get("custom_id")
if custom_id == "reply_send":
await interaction.response.send_modal(SendModal())
elif custom_id == "mute":
await interaction.response.defer(ephemeral=True)
thread_name = interaction.channel.name
try:
user_id = thread_name.split("(")[1].removesuffix(")")
mute_members.append(str(user_id))
except Exception:
await interaction.followup.send(content="ミュートできませんでした。", ephemeral=True)
return
msg = await interaction.channel.send(content=f"このユーザーをミュートしました。\n-# {interaction.user.mention}がミュートしました。", view=discord.ui.View(timeout=None).add_item(discord.ui.Button(label="ミュート解除", custom_id="unmute", style=discord.ButtonStyle.red)))
await msg.pin()
await interaction.followup.send(content="ミュートしました。", ephemeral=True)
elif custom_id == "unmute":
await interaction.response.defer(ephemeral=True)
thread_name = interaction.channel.name
try:
user_id = thread_name.split("(")[1].removesuffix(")")
mute_members.remove(str(user_id))
except Exception:
await interaction.followup.send(content="ミュートを解除できませんでした。", ephemeral=True)
return
msg = await interaction.channel.send(content=f"このユーザーのミュートを解除しました。\n-# {interaction.user.mention}が解除しました。", view=discord.ui.View(timeout=None).add_item(discord.ui.Button(label="ミュート", custom_id="mute", style=discord.ButtonStyle.red)))
await msg.pin()
await interaction.followup.send(content="ミュートを解除しました。", ephemeral=True)
@bot.event
async def on_message(message: discord.Message):
if message.author.bot:
return
if message.guild:
return
thread_name = f"{message.author.name} ({message.author.id})"
channel = bot.get_channel(1523887748287692850)
if not channel:
print("指定されたチャンネルが見つかりません。")
return
if str(message.author.id) in mute_members:
return
webhooks = await channel.webhooks()
webhook = discord.utils.get(webhooks, name="ModMail")
if not webhook:
webhook = await channel.create_webhook(name="ModMail")
target_thread = None
for t in channel.threads:
if t.name == thread_name:
target_thread = t
break
if not target_thread:
target_thread = await channel.create_thread(
name=thread_name,
type=discord.ChannelType.public_thread
)
button_message = button_messages.get(thread_name)
if button_message:
try:
await button_message.delete()
except discord.NotFound:
pass
await webhook.send(
content=message.content[:1800],
allowed_mentions=discord.AllowedMentions.none(),
thread=target_thread,
avatar_url=message.author.display_avatar.url,
username=message.author.name
)
view = discord.ui.View(timeout=None)
view.add_item(discord.ui.Button(label="送信", custom_id="reply_send", style=discord.ButtonStyle.primary))
view.add_item(discord.ui.Button(label="ミュート", custom_id="mute", style=discord.ButtonStyle.red))
button_messages[thread_name] = await target_thread.send(view=view, content="送信は返信は下のボタンから行えます。")
bot.run(os.environ.get('TOKEN'))