Reputation: 89
I have a problem with my bot joining voice channel.
Code:
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);
}
}
}
I've done everything like it's written in the documentation here.
It executes the SendMessange command but it doesn't join the voice channel.
But Visual Studio says: here.
Did I make a mistake?
Thank you for your answers.
Upvotes: 2
Views: 8173
Reputation: 32
Call this right under your class myBot
,
IVoiceChannel channel;
IAudioClient client;
Try using this in your music command, if you need more information, I can share my entire Music Module but this should do the trick.
IVoiceChannel channel = (Context.User as IVoiceState).VoiceChannel;
IAudioClient client = await channel.ConnectAsync();
Upvotes: 0
Reputation: 11
Does your server called "VoiceChannel"?
If not, than by calling
discord.FindServers("VoiceChannel")
your client, most likely, founds nothing (null
) instead of collection of servers, and tries to fetch voice channels from First() of them
for example, if your server named "My server" and has voice channel named "VoiceChannel" you can use this construction to get your voice channel:
discord.Servers.Single(s => s.Name == "My server").VoiceChannels.Single(v => v.Name == "VoiceChannel")
Upvotes: 1