-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
95 lines (85 loc) · 3.69 KB
/
main.py
File metadata and controls
95 lines (85 loc) · 3.69 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
import discord
import pyaudio
from config import super_secret_token
# sword, party
the_channels = [1151755897366904914, 1151092359048724544]
class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.party_connected = False
self.sword_connected = False
self.p = None
self.SPEAKERS = None
self.stream = None
def setup_pyaudio(self):
self.p = pyaudio.PyAudio()
self.SPEAKERS = self.p.get_default_output_device_info()["index"]
self.stream = self.p.open(format=pyaudio.paInt16,
channels=2,
rate=48000,
input=True,
frames_per_buffer=1024,
# TODO - Is this actually a problem?
input_device_index=self.SPEAKERS)
async def on_ready(self, member):
for each_channel in the_channels:
try:
each_channel = int(each_channel)
except ValueError:
print(f'ERROR: {each_channel} is not a channel integer.')
else:
this_channel = client.get_channel(each_channel)
if isinstance(this_channel, discord.VoiceChannel):
if len(this_channel.members) == 0:
pass
elif len(this_channel.members) >= 1:
found_bot = False
for each_user in this_channel.members:
if each_user.id == 1151658242108305539:
found_bot = True
if not found_bot:
await this_channel.connect()
else:
voice = member.guild.voice_client
await voice.disconnect(force=True)
async def on_voice_state_update(self, member, before, after):
found_bot = False
for each_channel in the_channels:
try:
each_channel = int(each_channel)
except ValueError:
print(f'ERROR: {each_channel} is not a channel integer.')
else:
this_channel = client.get_channel(each_channel)
for each_user in this_channel.members:
if each_user.id == 1151658242108305539:
found_bot = True
if len(this_channel.members) == 0:
# We probably just left, so don't do anything
pass
elif len(this_channel.members) == 1:
if after.channel is None:
voice = member.guild.voice_client
await voice.disconnect(force=True)
else:
if not found_bot:
await this_channel.connect()
else:
if not found_bot:
await this_channel.connect()
async def play_music(self, member):
while self.in_voice:
if self.p is None and self.SPEAKERS is None and self.stream is None:
self.setup_pyaudio()
music_data = self.stream.read(1024, exception_on_overflow=False)
voice = member.guild.voice_client
# TODO - Figure out the encode param here
voice.send_audio_packet(music_data, encode=the_music.is_opus())
self.stream.close()
self.p.terminate()
self.p = None
self.SPEAKERS = None
self.stream = None
intents = discord.Intents.default()
client = MyClient(intents=intents)
client.run(super_secret_token)