DSharp music bot does not play music

22 Views Asked by At

I started writing a Discord bot in C#, but I ran into a problem when playing the sound. The bot senses the command and joins the room, but it doesn't play any sound, and I don't know what's causing it.

AudioCommands.cs:

[Command("play")]
public async Task PlayMusic(CommandContext ctx, string query)
{
    var userVC = ctx.Member.VoiceState.Channel;
    var lavalinkInstance = ctx.Client.GetLavalink();
    if (ctx.Member.VoiceState == null || userVC == null) { await ctx.Message.RespondAsync("You need to join voice"); return; }
    if(!lavalinkInstance.ConnectedNodes.Any()) { await ctx.Message.RespondAsync("You need to join voice"); return; }
    if(userVC.Type != DSharpPlus.ChannelType.Voice) { await ctx.Message.RespondAsync("You need to join voice."); return; }
    var node = lavalinkInstance.ConnectedNodes.Values.First();
    await node.ConnectAsync(userVC);
    var conn = node.GetGuildConnection(ctx.Member.VoiceState.Guild);
    if (conn == null) { await ctx.Message.RespondAsync("[CONNECTION FAILED]"); return; }
    var searchQuery = await node.Rest.GetTracksAsync(query);
    if (searchQuery.LoadResultType == LavalinkLoadResultType.NoMatches || searchQuery.LoadResultType == LavalinkLoadResultType.LoadFailed)
    {
        await ctx.Message.RespondAsync("Music not found");
        return;
    }
    var musicTrack = searchQuery.Tracks.First();
    await conn.PlayAsync(musicTrack);
    string musicDescription = $"Now playing: {musicTrack.Title}";
    var nowPlayingEmbed = new DiscordEmbedBuilder()
    {
        Color = DiscordColor.Orange,
        Title = $"I connected and play music.",
        Description = musicDescription
    };

    await ctx.Message.RespondAsync(embed: nowPlayingEmbed);
}

Program.cs:

private static DiscordClient Client { get; set; }
private static CommandsNextExtension Commands { get; set; }
private static LavalinkExtension Lavalink { get; set; }
static async Task Main(string[] args)
{
    var jsonReader = new JSONReader();
    await jsonReader.ReadJSON();
    var discordConfig = new DiscordConfiguration()
    {
        Intents = DiscordIntents.All,
        Token = jsonReader.token,
        TokenType = TokenType.Bot,
        AutoReconnect = true
    };
    Client = new DiscordClient(discordConfig);
    Client.Ready += Client_Ready;
    var commandsConfig = new CommandsNextConfiguration()
    {
        StringPrefixes = new string[] { jsonReader.prefix },
        EnableMentionPrefix = true,
        EnableDms = true,
        EnableDefaultHelp = false
    };
    Commands = Client.UseCommandsNext(commandsConfig);

    Commands.RegisterCommands<FunCommands>();
    Commands.RegisterCommands<AudioCommands>();

    var endpoint = new ConnectionEndpoint { Hostname = "lavalink.devamop.in", Port = 80, Secured = false };
    var lavalinkConfig = new LavalinkConfiguration { Password = "****", RestEndpoint = endpoint, SocketEndpoint = endpoint};

    var lavalink = Client.UseLavalink();
    await Client.ConnectAsync();
    await lavalink.ConnectAsync(lavalinkConfig);
    /*If Lavalink host doesnt work, try another one: https://lavalink.darrennathanael.com/SSL/lavalink-with-ssl/ */
    await Task.Delay(-1);
}
private static Task Client_Ready(DiscordClient sender, DSharpPlus.EventArgs.ReadyEventArgs args) { return Task.CompletedTask; }

Can you please help?

I have tried several Lavalink hosts.

0

There are 0 best solutions below