-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
118 lines (105 loc) · 4 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
# StageMusicBot - https://discord.gg/qBq2WSsgvv
import asyncio, json, logging, os, discord, config, mutagen
from discord.ext import commands
from discord.ext.commands.errors import CommandInvokeError
from src import get_info
logging.basicConfig(level=logging.WARN, filemode="w")
guilds = []
directory = os.getcwd()
if os.path.exists("./albumart.json"):
with open("./albumart.json", "r") as cjson:
albumart = json.load(cjson)
else:
albumart = None
bot = commands.Bot(
command_prefix=commands.when_mentioned_or(config.PREFIX),
intents=discord.Intents.all(),
)
@bot.event
async def on_ready():
global save_guild
if not os.path.exists("./songs"):
logging.warning(
'Unable to find "songs" directory. Please ensure there is a "songs" directory present at the same level as this file'
)
return
logging.info(f"{bot.user} has connected to Discord!")
while len(guilds) < 1:
async for guild in bot.fetch_guilds(limit=5):
save_guild = guild
guilds.append(guild.name)
text_channel_list = []
for guild in bot.guilds:
for channel in guild.stage_channels:
text_channel_list.append(channel)
stage = discord.utils.get(text_channel_list, name=config.STAGENAME)
channel = stage.name
global Vc
global Tune
try:
Vc = await stage.connect()
member = guild.me
await member.edit(suppress=False)
except CommandInvokeError:
pass
while True:
while Vc.is_playing():
await asyncio.sleep(1)
else:
Tune = get_info.write_song()
Vc.play(discord.FFmpegPCMAudio(f"songs/{Tune}"))
Vc.cleanup()
audiofile = mutagen.File(f"songs/{Tune}")
title = audiofile.get("title")[0]
await bot.change_presence(activity=discord.Game(name=f"{title}"))
Vc.source = discord.PCMVolumeTransformer(Vc.source, volume=config.VOLUME)
if "suppress=False" in str(stage.voice_states):
pass
else:
await member.edit(suppress=False)
@bot.event
async def on_voice_state_update(name, past, current):
if current.channel == None and name.name == bot.user.id:
logging.warning("Shutting down due to disconnect")
logging.shutdown()
await bot.close()
else:
# In this situation, the bot MAY still be disconnected, however I'm not sure on the fix for that unless I rewrite this first. It's a massive mess, but so far, an improvement
pass
@bot.command(name="close")
async def close(ctx):
logging.warning("Shutting down via command")
logging.shutdown()
await bot.close()
@bot.command(
name="nowplaying",
description="Command to check what song is currently playing",
aliases=["np"],
)
async def nowplaying(ctx):
try:
if not Vc.is_playing():
await ctx.reply("I need to play something first")
except:
await ctx.reply("I need to play something first")
else:
song_info = get_info.info(Tune)
embed = discord.Embed(color=0xC0F207)
embed.set_author(name="Now Playing 🎶", icon_url=ctx.guild.icon.url).add_field(
name="Playing", value=f"{song_info[1]} - {song_info[0]}", inline=False
).set_footer(
text="This bot is still in development, if you have any queries, please contact the owner",
icon_url=(ctx.message.author.avatar.url),
)
if song_info[2] is not None:
embed.add_field(name="Album", value=f"{song_info[2]}", inline=True)
if albumart is not None:
try:
embed.set_thumbnail(url=albumart[song_info[2]])
except KeyError:
logging.warning("No Albumart found")
pass
else:
pass
await ctx.reply(embed=embed)
bot.run(config.TOKEN, reconnect=True)