-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.pyw
executable file
·172 lines (138 loc) · 3.87 KB
/
main.pyw
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
import logging
# error logging
error_formatter = logging.Formatter(
fmt="%(asctime)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
)
error_handler = logging.FileHandler("DAP_errors.log", delay=True)
error_handler.setLevel(logging.ERROR)
error_handler.setFormatter(error_formatter)
base_logger = logging.getLogger()
base_logger.addHandler(error_handler)
import sys
import cli
import sound
import asyncio
import discord
import argparse
# commandline args
parser = argparse.ArgumentParser(description="Discord Audio Pipe")
connect = parser.add_argument_group("Command Line Mode")
query = parser.add_argument_group("Queries")
parser.add_argument(
"-t",
"--token",
dest="token",
action="store",
default=None,
help="The token for the bot",
)
parser.add_argument(
"-v",
"--verbose",
dest="verbose",
action="store_true",
help="Enable verbose logging",
)
connect.add_argument(
"-c",
"--channel",
dest="channel",
action="store",
type=int,
help="The channel to connect to as an id",
)
connect.add_argument(
"-d",
"--device",
dest="device",
action="store",
type=int,
help="The device to listen from as an index",
)
query.add_argument(
"-D",
"--devices",
dest="query",
action="store_true",
help="Query compatible audio devices",
)
query.add_argument(
"-C",
"--channels",
dest="online",
action="store_true",
help="Query servers and channels (requires token)",
)
args = parser.parse_args()
is_gui = not any([args.channel, args.device, args.query, args.online])
# verbose logs
if args.verbose:
debug_formatter = logging.Formatter(
fmt="%(asctime)s:%(levelname)s:%(name)s: %(message)s"
)
debug_handler = logging.FileHandler(
filename="discord.log", encoding="utf-8", mode="w"
)
debug_handler.setFormatter(debug_formatter)
debug_logger = logging.getLogger("discord")
debug_logger.setLevel(logging.DEBUG)
debug_logger.addHandler(debug_handler)
# don't import qt stuff if not using gui
if is_gui:
import gui
from PyQt5.QtWidgets import QApplication, QMessageBox
app = QApplication(sys.argv)
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
# main
async def main(bot):
try:
# query devices
if args.query:
for device, index in sound.query_devices().items():
print(index, device)
return
# check for token
token = args.token
if token is None:
token = open("token.txt", "r").read()
# query servers and channels
if args.online:
await cli.query(bot, token)
return
# GUI
if is_gui:
bot_ui = gui.GUI(app, bot)
asyncio.ensure_future(bot_ui.ready())
asyncio.ensure_future(bot_ui.run_Qt())
# CLI
else:
asyncio.ensure_future(cli.connect(bot, args.device, args.channel))
await bot.start(token)
except FileNotFoundError:
if is_gui:
msg.setWindowTitle("Token Error")
msg.setText("No Token Provided")
msg.exec()
else:
print("No Token Provided")
except discord.errors.LoginFailure:
if is_gui:
msg.setWindowTitle("Login Failed")
msg.setText("Please check if the token is correct")
msg.exec()
else:
print("Login Failed: Please check if the token is correct")
except Exception:
logging.exception("Error on main")
# run program
bot = discord.Client(intents=discord.Intents.default())
loop = asyncio.get_event_loop_policy().get_event_loop()
try:
loop.run_until_complete(main(bot))
except KeyboardInterrupt:
print("Exiting...")
loop.run_until_complete(bot.close())
# this sleep prevents a bugged exception on Windows
loop.run_until_complete(asyncio.sleep(1))
loop.close()