-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
207 lines (173 loc) · 8.59 KB
/
main.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import asyncio
import datetime
import importlib
import os
import random
import time
import warnings
import discord
import youtube_dl
from discord.utils import get
from globals import get_setting, language_pack, tickets
from helpers.create_ticket import run as create_ticket
from statics import config as conf
from statics import init
from statics import messages
warnings.simplefilter("ignore")
print("starting...")
class ClientClass(discord.Client):
@staticmethod
async def on_message(message):
if message.author.id == client.user.id:
return
if message.content == "!help" and isinstance(message.channel, discord.abc.PrivateChannel):
owner_of_guild_id = None
for guild in client.guilds:
if guild.owner == message.author:
owner_of_guild_id = guild.id
break
if owner_of_guild_id is None:
await message.channel.send("", embed=messages.En.help_private)
else:
await message.channel.send("", embed=language_pack(owner_of_guild_id, "help_private"))
return
if message.channel is discord.DMChannel:
return
prefix = get_setting(message.guild.id, "prefix")
if str(message.content).startswith(prefix):
invoke = str(message.content).split(" ")[0][len(prefix):]
if list_commands.__contains__("command_" + invoke):
args = str(message.content).split(" ")
del args[0]
print("------- new command handle -------")
print(invoke)
print(args)
module = importlib.import_module("commands.command_" + invoke)
await getattr(module, "execute")(client, message, args)
@staticmethod
async def on_guild_join(guild):
print("joined the Server: %s" % guild.name)
print("creating configs...")
init.init_db(client)
await guild.owner.send("", embed=language_pack(guild.id).welcome_private)
@staticmethod
async def on_ready():
print("Bot started succesfully...\n")
init.init_db(client)
print("trying to register commands...")
global list_commands
list_commands = []
for file in os.listdir("commands/"):
if file.endswith(".py"):
if file.startswith("command_"):
module = file[:-3]
print("adding module/command to register: " + module)
list_commands.append(module)
print("found %s commands" % len(list_commands))
print(list_commands)
@staticmethod
async def on_voice_state_update(member, before, after):
supportchannel = get_setting(str(member.guild.id), "supportchannel")
voice = get(client.voice_clients, guild=member.guild)
channel = member.guild.get_channel(int(supportchannel))
join = False
leave = False
if after.channel is None:
if before.channel is not None:
if str(before.channel.id) == supportchannel:
leave = True
elif str(after.channel.id) != supportchannel:
if before.channel is None:
pass
elif str(before.channel.id) == supportchannel:
leave = True
elif str(after.channel.id) == supportchannel:
join = True
if member.id == client.user.id:
return
if join:
print(member.name, "joined support channel.")
create_ticket(member)
await member.send("", embed=discord.Embed(title="Your ticket has been created!",
description="You will be automatically moved as soon as a supporter accepts your ticket.\n"
"You can enjoy some music while you are waiting.",
color=discord.Color.purple()))
supporter_chat = await client.fetch_channel(get_setting(member.guild.id, "supporttext"))
emb = discord.Embed(title=member.name+" opened ticket #"+str(tickets[member.id]["id"])+".",
description="<@"+str(member.id)+"> is waiting in the queue.\n"
"Claim the ticket and move him to your voice channel by reacting to this message.",
color=discord.Color.green())
ticket_message = await supporter_chat.send("", embed=emb)
await ticket_message.add_reaction("✅")
tickets[member.id]["message"] = ticket_message
if voice is None:
voice = await channel.connect()
elif not voice.is_connected():
voice = await channel.connect()
else:
await voice.move_to(channel)
with youtube_dl.YoutubeDL({}) as ydl:
song_info = ydl.extract_info(get_setting(member.guild.id, "supportvideo"), download=False)
url = song_info["formats"][1]["url"]
while voice.is_connected():
try:
voice.play(discord.FFmpegPCMAudio(url, before_options=" -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5"))
except discord.errors.ClientException:
pass
while voice.is_playing():
await asyncio.sleep(1)
voice.stop()
await asyncio.sleep(1)
if leave:
try:
try:
supporttext = await client.fetch_channel(get_setting(member.guild.id, "supporttext"))
ticket_msg = await supporttext.fetch_message(tickets[member.id]["message"].id)
await ticket_msg.delete()
except discord.errors.NotFound:
pass
del tickets[member.id]
except KeyError:
pass
print(member.name, "left support channel.")
if voice is not None:
for key in tickets.keys():
if tickets[key]["guild"] == channel.guild.id:
return
await voice.disconnect()
return
@staticmethod
async def on_reaction_add(reaction, user):
if user.id == client.user.id:
return
if reaction.emoji == "✅" and str(reaction.message.channel.id) == get_setting(reaction.message.guild.id, "supporttext") and reaction.message.author.id == client.user.id:
role_names = [role.name for role in user.roles]
if "Support" in role_names:
if user.voice is not None:
for key in tickets.keys():
if tickets[key]["message"].id == reaction.message.id:
ticket = tickets[key]
break
await reaction.message.delete()
await reaction.message.channel.send("", embed=discord.Embed(title="Ticket #"+str(ticket["id"])+" closed.",
description="Author: <@"+str(key)+">\n"
"Opened: " + ticket["time"] + "\n"
"Closed: " + str(time.strftime("%Y-%m-%d %H:%M:%S")) + "\n"
"Claimed by: <@"+str(user.id)+">\n",
color=discord.Color.gold()))
await reaction.message.channel.guild.get_member(key).move_to(user.voice.channel)
else:
error_message = await reaction.message.channel.send("", embed=discord.Embed(title="",
description="🚫 You must be in a voice channel to claim a ticket.",
color=discord.Color.red()))
await reaction.remove(user)
time.sleep(2)
await error_message.delete()
else:
await reaction.remove(user)
return
else:
return
return
client = ClientClass()
client.run(conf.bot.get("token"))