KasinoBot/PlayerController.cs

81 lines
2.5 KiB
C#

using Discord.Audio;
using Discord.Commands;
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KasinoBot
{
public class PlayerController
{
private static PlayerController instance;
public static PlayerController Instance
{
get
{
if (instance == null)
instance = new PlayerController();
return instance;
}
}
private static Dictionary<SocketGuild, Music> guildMusic = new Dictionary<SocketGuild, Music>();
private static Dictionary<SocketGuild, IAudioClient> clientAudios = new Dictionary<SocketGuild, IAudioClient>();
private PlayerController() { }
public async Task Play(string musicInfo, SocketCommandContext context)
{
var guild = context.Guild;
var user = context.User;
var message = context.Message;
if (guildMusic.ContainsKey(guild))
{
await guildMusic[guild].Play(musicInfo, user, message);
}
else
{
Music musicPlayer;
IAudioClient? audioClient;
if (!clientAudios.TryGetValue(guild, out audioClient))
{
SocketVoiceChannel? voiceChannel =
guild.Channels.Select(x => x as SocketVoiceChannel).Where(x => x?.Users.Contains(user) ?? false).FirstOrDefault();
if (voiceChannel == null)
{
await context.Channel.SendMessageAsync("You need to be in a voice channel to play music.");
return;
}
audioClient = await voiceChannel.ConnectAsync();
clientAudios.Add(guild, audioClient);
}
musicPlayer = new Music(guild, context.Channel, audioClient);
guildMusic.Add(guild, musicPlayer);
await guildMusic[guild].Play(musicInfo, user, message);
}
}
public async Task Stop(SocketCommandContext context)
{
if (guildMusic.ContainsKey(context.Guild))
await guildMusic[context.Guild].Stop();
}
public async Task Skip(SocketCommandContext context)
{
if (guildMusic.ContainsKey(context.Guild))
await guildMusic[context.Guild].Skip();
}
}
}