gpt4 book ai didi

c# - Discord Bot (C#) 加入语音 channel 时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-03 12:32:52 24 4
gpt4 key购买 nike

我的机器人加入语音 channel 时遇到问题。

代码:

    using Discord;
using Discord.Commands;
using Discord.Audio;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DodoBot
{
class MyBot
{
DiscordClient discord;
CommandService commands;
Random rand;
string[] cats = new string[]
{
"cate.jpg",
"gut.jpg",
"meh.jpg",
"ugly.jpg",
"wow.jpg",
};

public MyBot()
{
rand = new Random();
discord = new DiscordClient(x =>
{
x.LogLevel = LogSeverity.Info;
x.LogHandler = Log;
});

discord.UsingCommands(x =>
{
x.PrefixChar = '!';
x.AllowMentionPrefix = true;
});

commands = discord.GetService<CommandService>();

RegisterHiCommand();
RegisterCatdCommand();
RegisterCatCommand();

OnJoin();
OnLeave();

discord.UsingAudio(x =>
{
x.Mode = AudioMode.Outgoing;
RegisterJoinVoiceCommand();
});

discord.ExecuteAndWait(async () =>
{
await discord.Connect("MyToken", TokenType.Bot);
});
}

private void RegisterJoinVoiceCommand()
{
commands.CreateCommand("summon")
.Do(async (e) =>
{
await e.Channel.SendMessage("```Joining masta!```");
await discord.GetService<AudioService>().Join(discord.FindServers("VoiceChannel").FirstOrDefault().VoiceChannels.FirstOrDefault());
});
}

private void RegisterCatdCommand()
{
commands.CreateCommand("catd")
.Do(async (e) =>
{
Message[] msg2Del;
msg2Del = await e.Channel.DownloadMessages(1);
await e.Channel.DeleteMessages(msg2Del);
int imgIndex = rand.Next(cats.Length);
await e.Channel.SendFile("Cats/"+cats[imgIndex]);
});
}
private void RegisterCatCommand()
{
commands.CreateCommand("cat")
.Do(async (e) =>
{
int imgIndex = rand.Next(cats.Length);
await e.Channel.SendFile("Cats/" + cats[imgIndex]);
});
}
private void RegisterHiCommand()
{
commands.CreateCommand("hi")
.Do(async (e) =>
{
await e.Channel.SendMessage("HelloWorld!");
});
}

private void OnJoin()
{
discord.UserJoined += async (s, e) =>
{
var channel = e.Server.FindChannels("general").FirstOrDefault();
var user = e.User.Name;
await channel.SendMessage(string.Format("@"+user + " has joined!"));
};
}

private void OnLeave()
{
discord.UserLeft += async (s, e) =>
{
var channel = e.Server.FindChannels("general").FirstOrDefault();
var user = e.User.Name;
await channel.SendMessage(string.Format("@"+user + " has left!"));
};
}

private void Log(object sender, LogMessageEventArgs e)
{
Console.WriteLine(e.Message);
}
}
}

我已经按照文档中的描述完成了所有操作 here .

它执行 SendMessange 命令但不加入语音 channel 。

但是 Visual Studio 说: here .

我做错了吗?

感谢您的回答。

最佳答案

您的服务器是否调用了“VoiceChannel”?

如果没有,则通过调用discord.FindServers("VoiceChannel")

您的客户端很可能找不到任何东西(null)而不是服务器集合,并尝试从其中的 First() 获取语音 channel

例如,如果您的服务器名为“我的服务器”并且语音 channel 名为“VoiceChannel”,您可以使用此构造来获取您的语音 channel :discord.Servers.Single(s => s.Name == "My server").VoiceChannels.Single(v => v.Name == "VoiceChannel")

关于c# - Discord Bot (C#) 加入语音 channel 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42051697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com