discord.net框架会说话的时钟bot音频无法正常工作,并且bot有时会在1秒后离开语音通道

时间:2020-08-19 22:38:06

标签: c# bots discord.net

该机器人是一个会说话的时钟,您使用命令时间,并且从我的计算机上的钥匙上的光盘上获取时分分钟的语音文件,但该机器人并不总是加入vc(有时会加入并且只是离开)。 该机器人也没有播放任何语音线路,我使用ffmpeg,opus和libsodium,但该机器人似乎没有播放。这是C#代码的一部分(其他部分只是测试文本命令...)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using Discord.Audio;
        [Command("join", RunMode = RunMode.Async)]
        public async Task JoinChannel(IVoiceChannel channel = null)
        {
            channel = channel ?? (Context.Message.Author as IGuildUser)?.VoiceChannel;
            if (channel == null) { await Context.Message.Channel.SendMessageAsync("User must be in a voice channel, or a voice channel must be passed as an argument."); return; }
        }
        [Command("time", RunMode = RunMode.Async)]
        public async Task time([Remainder]int gmt)
        {
            IVoiceChannel channel = (Context.Message.Author as IGuildUser)?.VoiceChannel;
            var audioClient = await channel.ConnectAsync();
            DateTime time = DateTime.Now;
            int hours = time.Hour - 2 + gmt;
            int minutes = time.Minute;
            bool i = hours < 12;
            await SendAsync(audioClient, @"E:\sounds\intro.wav");
            await SendAsync(audioClient, @"E:\hours\" + hours + ".wav");
            await SendAsync(audioClient, @"E:\numbers\" + minutes + ".wav");
            await SendAsync(audioClient, @"E:\sounds\time" + i + ".wav");
        }

        private Process CreateStream(string path)
        {
            return Process.Start(new ProcessStartInfo
            {
                FileName = "ffmpeg.exe",
                Arguments = $"-hide_banner -loglevel panic -i \"{path}\" -ac 2 -f s16le -ar 48000 pipe:1",
                UseShellExecute = false,
                RedirectStandardOutput = true,
            });
        }

        private async Task SendAsync(IAudioClient client, string path)
        {
            using (var ffmpeg = CreateStream(path))
            using (var output = ffmpeg.StandardOutput.BaseStream)
            using (var discord = client.CreatePCMStream(AudioApplication.Mixed))
            {
                try { await output.CopyToAsync(discord); }
                finally { await discord.FlushAsync(); }
            }
        }

0 个答案:

没有答案