-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudio.py
103 lines (92 loc) · 4.42 KB
/
Audio.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
# ------------------------------------------------------------------------------
# MidnightStarshineBot - a multipurpose Discord bot
# Copyright (C) 2020 T. Duke Perry
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# ------------------------------------------------------------------------------
import discord
import datetime
import os
from Constants import *
from Utilities import *
AMBIENCE_DIRECTORY = "Ambience"
AMBIENCE_FILE_EXT = "opus"
START_AMBIENCE_COMMAND = "startambience"
STOP_AMBIENCE_COMMAND = "stopambience"
LIST_AMBIENCE_COMMAND = "listambience"
connectionCache = dict()
ambienceList = []
if os.path.exists(AMBIENCE_DIRECTORY):
for root, dirs, files in os.walk(AMBIENCE_DIRECTORY):
for filename in files:
if filename.endswith("." + AMBIENCE_FILE_EXT):
ambienceList.append(filename[:-len(AMBIENCE_FILE_EXT) - 1])
async def listAmbience(message):
if len(ambienceList) > 0:
list = ""
for track in ambienceList:
list += track + "\n"
await message.channel.send("Here's a list of all available ambience tracks:\n```\n" + list + "```")
async def startAmbience(message, commandArgs):
if len(ambienceList) > 0:
voiceState = message.author.voice
if voiceState is None:
await message.channel.send("You need to be connected to a voice channel.")
else:
botVoiceState = message.guild.me.voice
if message.guild.me.voice is not None:
await message.channel.send("Already connected to a voice channel.")
elif commandArgs not in ambienceList:
await message.channel.send("Cannot find specified track. Try running `" + getPrefix(message.guild) + LIST_AMBIENCE_COMMAND + "`.")
else:
if message.guild.id in connectionCache:
await connectionCache[message.guild.id].disconnect()
del connectionCache[message.guild.id]
vc = await voiceState.channel.connect()
try:
def loopAudio(error):
if error is None and vc.is_connected():
vc.play(discord.FFmpegOpusAudio(AMBIENCE_DIRECTORY + "/" + commandArgs + "." + AMBIENCE_FILE_EXT), after=loopAudio)
vc.play(discord.FFmpegOpusAudio(AMBIENCE_DIRECTORY + "/" + commandArgs + "." + AMBIENCE_FILE_EXT), after=loopAudio)
except:
await vc.disconnect()
raise
else:
connectionCache[message.guild.id] = vc
await message.channel.send("Playing `" + commandArgs + "`")
async def stopAmbience(message, client):
if len(ambienceList) > 0:
if message.guild.me.voice is None:
await message.channel.send("Not connected to a voice channel.")
else:
if message.guild.id in connectionCache:
await connectionCache[message.guild.id].disconnect()
del connectionCache[message.guild.id]
else:
for connection in client.voice_clients:
if connection.channel.guild.id == message.guild.id:
await connection.disconnect()
break
await message.channel.send("Disconnected from Voice Channel.")
async def checkVoiceChannel(server, client):
if server.me.voice is not None:
if server.id not in connectionCache:
for connection in client.voice_clients:
if connection.channel.guild.id == server.id:
connectionCache[server.id] = connection
break
vc = connectionCache[server.id]
if len(vc.channel.members) <= 1:
await vc.disconnect()
del connectionCache[server.id]