music tweaks, something is wrong.Kick/ban messages. closes #75

This commit is contained in:
Master Kwoth 2016-03-04 17:37:54 +01:00
parent 2d949333b5
commit c1ccc7d4d2
4 changed files with 68 additions and 38 deletions

View File

@ -43,8 +43,7 @@ namespace NadekoBot.Classes.Music {
public Channel PlaybackVoiceChannel { get; private set; } public Channel PlaybackVoiceChannel { get; private set; }
private bool Stopped { get; set; } private bool Stopped { get; set; } = false;
private readonly object disconnectLock = new object();
public MusicPlayer(Channel startingVoiceChannel, float? defaultVolume) { public MusicPlayer(Channel startingVoiceChannel, float? defaultVolume) {
if (startingVoiceChannel == null) if (startingVoiceChannel == null)
@ -99,7 +98,7 @@ namespace NadekoBot.Classes.Music {
} }
} }
public void Stop(bool disconnect = false) { public void Stop() {
lock (playlistLock) { lock (playlistLock) {
playlist.Clear(); playlist.Clear();
if (!SongCancelSource.IsCancellationRequested) if (!SongCancelSource.IsCancellationRequested)
@ -178,6 +177,7 @@ namespace NadekoBot.Classes.Music {
if (!SongCancelSource.IsCancellationRequested) if (!SongCancelSource.IsCancellationRequested)
SongCancelSource.Cancel(); SongCancelSource.Cancel();
try { try {
Stopped = true;
audioClient.Disconnect(); audioClient.Disconnect();
} }
catch {} catch {}

View File

@ -124,7 +124,7 @@ namespace NadekoBot.Classes.Music {
$"**【 {SongInfo.Title.TrimTo(55)} 】**`{(SongInfo.Provider ?? "-")}`"; $"**【 {SongInfo.Title.TrimTo(55)} 】**`{(SongInfo.Provider ?? "-")}`";
public SongInfo SongInfo { get; } public SongInfo SongInfo { get; }
private PoopyBuffer songBuffer { get; } = new PoopyBuffer(10.MiB()); private PoopyBuffer songBuffer { get; } = new PoopyBuffer(4.MiB());
private bool prebufferingComplete { get; set; } = false; private bool prebufferingComplete { get; set; } = false;
public MusicPlayer MusicPlayer { get; set; } public MusicPlayer MusicPlayer { get; set; }
@ -149,35 +149,38 @@ namespace NadekoBot.Classes.Music {
var buffer = new byte[blockSize]; var buffer = new byte[blockSize];
var attempt = 0; var attempt = 0;
while (!cancelToken.IsCancellationRequested) { while (!cancelToken.IsCancellationRequested) {
var read = await p.StandardOutput.BaseStream.ReadAsync(buffer, 0, blockSize, cancelToken); var read = 0;
try {
read = await p.StandardOutput.BaseStream.ReadAsync(buffer, 0, blockSize, cancelToken);
} catch {
return;
}
if (read == 0) if (read == 0)
if (attempt++ == 20) if (attempt++ == 20)
break; break;
else else
await Task.Delay(50, cancelToken); await Task.Delay(40, cancelToken);
else else
attempt = 0; attempt = 0;
await songBuffer.WriteAsync(buffer, read, cancelToken); await songBuffer.WriteAsync(buffer, read, cancelToken);
if (songBuffer.ContentLength > 2.MB()) if (songBuffer.ContentLength > 2.MB())
prebufferingComplete = true; prebufferingComplete = true;
} }
} catch { } catch (Exception ex) {
Console.WriteLine("Buffering errored"); Console.WriteLine($"Buffering errored: {ex.Message}");
} finally { } finally {
Console.WriteLine($"Buffering done." + $" [{songBuffer.ContentLength}]"); Console.WriteLine($"Buffering done." + $" [{songBuffer.ContentLength}]");
if (p != null) { if (p != null) {
try { try {
p.Kill(); p.Kill();
p.WaitForExit(); } catch { }
}
catch {}
p.Dispose(); p.Dispose();
} }
} }
}); });
internal async Task Play(IAudioClient voiceClient, CancellationToken cancelToken) { internal async Task Play(IAudioClient voiceClient, CancellationToken cancelToken) {
var bufferTask = BufferSong(cancelToken); var bufferTask = BufferSong(cancelToken).ConfigureAwait(false);
var bufferAttempts = 0; var bufferAttempts = 0;
const int waitPerAttempt = 500; const int waitPerAttempt = 500;
var toAttemptTimes = SongInfo.ProviderType != MusicType.Normal ? 5 : 9; var toAttemptTimes = SongInfo.ProviderType != MusicType.Normal ? 5 : 9;
@ -194,28 +197,33 @@ namespace NadekoBot.Classes.Music {
var read = songBuffer.Read(buffer, blockSize); var read = songBuffer.Read(buffer, blockSize);
if (read == 0) if (read == 0)
if (attempt++ == 20) { if (attempt++ == 20) {
Console.WriteLine("Nothing to read.");
voiceClient.Wait(); voiceClient.Wait();
await bufferTask;
return; return;
} else }
await Task.Delay(50); else
await Task.Delay(50, cancelToken);
else else
attempt = 0; attempt = 0;
while (this.MusicPlayer.Paused) while (this.MusicPlayer.Paused)
await Task.Delay(200, cancelToken); await Task.Delay(200, cancelToken);
buffer = AdjustVolume(buffer, read, MusicPlayer.Volume); buffer = AdjustVolume(buffer, MusicPlayer.Volume);
Console.WriteLine("ADJUST VOLUME ERROR");
voiceClient.Send(buffer, 0, read); voiceClient.Send(buffer, 0, read);
} }
await bufferTask;
voiceClient.Clear();
cancelToken.ThrowIfCancellationRequested(); cancelToken.ThrowIfCancellationRequested();
} }
//stackoverflow ftw //stackoverflow ftw
private static byte[] AdjustVolume(byte[] audioSamples, int dataLength, float volume) { private static byte[] AdjustVolume(byte[] audioSamples, float volume) {
if (Math.Abs(volume - 1.0f) < 0.01f) if (Math.Abs(volume - 1.0f) < 0.01f)
return audioSamples; return audioSamples;
var array = new byte[audioSamples.Length]; var array = new byte[audioSamples.Length];
for (var i = 0; i < dataLength; i += 2) { for (var i = 0; i < array.Length; i += 2) {
// convert byte pair to int // convert byte pair to int
short buf1 = audioSamples[i + 1]; short buf1 = audioSamples[i + 1];

View File

@ -20,6 +20,8 @@ namespace NadekoBot.Modules {
commands.Add(new PlayingRotate()); commands.Add(new PlayingRotate());
} }
public override string Prefix { get; } = ".";
public override void Install(ModuleManager manager) { public override void Install(ModuleManager manager) {
manager.CreateCommands("", cgb => { manager.CreateCommands("", cgb => {
@ -146,8 +148,7 @@ namespace NadekoBot.Modules {
await role.Edit(color: new Color(red, green, blue)); await role.Edit(color: new Color(red, green, blue));
await e.Channel.SendMessage($"Role {role.Name}'s color has been changed."); await e.Channel.SendMessage($"Role {role.Name}'s color has been changed.");
} catch (Exception ex) { } catch (Exception ex) {
await e.Channel.SendMessage("Error occured, most likely invalid parameters."); await e.Channel.SendMessage("Error occured, most likely invalid parameters or insufficient permissions.");
Console.WriteLine($".rolecolor error: {ex}");
} }
}); });
@ -166,35 +167,56 @@ namespace NadekoBot.Modules {
}); });
cgb.CreateCommand(".b").Alias(".ban") cgb.CreateCommand(".b").Alias(".ban")
.Parameter("everything", ParameterType.Unparsed) .Parameter("user", ParameterType.Required)
.Description("Bans a mentioned user.") .Parameter("msg", ParameterType.Optional)
.Description("Bans a user by id or name with an optional message.\n**Usage**: .b \"@some Guy\" Your behaviour is toxic.")
.Do(async e => { .Do(async e => {
try { var msg = e.GetArg("msg");
if (e.User.ServerPermissions.BanMembers && e.Message.MentionedUsers.Any()) { var user = e.GetArg("user");
var usr = e.Message.MentionedUsers.First(); if (e.User.ServerPermissions.BanMembers) {
await usr.Server.Ban(usr); var usr = e.Server.FindUsers(user).FirstOrDefault();
await e.Channel.SendMessage("Banned user " + usr.Name + " Id: " + usr.Id); if (usr == null) {
await e.Channel.SendMessage("User not found.");
return;
}
if (!string.IsNullOrWhiteSpace(msg)) {
await usr.SendMessage($"**You have been BANNED from `{e.Server.Name}` server.**\n" +
$"Reason: {msg}");
await Task.Delay(2000); // temp solution; give time for a message to be send, fu volt
}
try {
await e.Server.Ban(usr);
await e.Channel.SendMessage("Banned user " + usr.Name + " Id: " + usr.Id);
} catch {
await e.Channel.SendMessage("Error. Most likely I don't have sufficient permissions.");
} }
}
catch (Exception ex) {
} }
}); });
cgb.CreateCommand(".k").Alias(".kick") cgb.CreateCommand(".k").Alias(".kick")
.Parameter("user") .Parameter("user")
.Parameter("msg",ParameterType.Unparsed)
.Description("Kicks a mentioned user.") .Description("Kicks a mentioned user.")
.Do(async e => { .Do(async e => {
try { var msg = e.GetArg("msg");
if (e.User.ServerPermissions.KickMembers && e.Message.MentionedUsers.Any()) { var user = e.GetArg("user");
var usr = e.Message.MentionedUsers.FirstOrDefault(); if (e.User.ServerPermissions.KickMembers) {
if (usr == null) var usr = e.Server.FindUsers(user).FirstOrDefault();
return; if (usr == null) {
await e.Channel.SendMessage("User not found.");
return;
}
if (!string.IsNullOrWhiteSpace(msg)) {
await usr.SendMessage($"**You have been KICKED from `{e.Server.Name}` server.**\n" +
$"Reason: {msg}");
await Task.Delay(2000); // temp solution; give time for a message to be send, fu volt
}
try {
await usr.Kick(); await usr.Kick();
await e.Channel.SendMessage("Kicked user " + usr.Name + " Id: " + usr.Id); await e.Channel.SendMessage("Kicked user " + usr.Name + " Id: " + usr.Id);
} catch {
await e.Channel.SendMessage("Error. Most likely I don't have sufficient permissions.");
} }
} catch {
await e.Channel.SendMessage("No sufficient permissions.");
} }
}); });
cgb.CreateCommand(".mute") cgb.CreateCommand(".mute")

View File

@ -15,7 +15,7 @@ using NadekoBot.Commands;
namespace NadekoBot.Modules { namespace NadekoBot.Modules {
internal class Conversations : DiscordModule { internal class Conversations : DiscordModule {
private string firestr = "🔥 ด้้้้้็็็็็้้้้้็็็็็้้้้้้้้็็็็็้้้้้็็ด้้้้้็็็็็้้้้้็็็็็้้้้้้้้็็็็็้้้้้็็็็็้้้้้้้้็็็ด้้้้้็็็็็้้้้้็็็็็้้้้้้้้็็็็็้้้้้็็็็็้้้้ 🔥"; private const string firestr = "🔥 ด้้้้้็็็็็้้้้้็็็็็้้้้้้้้็็็็็้้้้้็็ด้้้้้็็็็็้้้้้็็็็็้้้้้้้้็็็็็้้้้้็็็็็้้้้้้้้็็็ด้้้้้็็็็็้้้้้็็็็็้้้้้้้้็็็็็้้้้้็็็็็้้้้ 🔥";
public Conversations() { public Conversations() {
commands.Add(new CopyCommand()); commands.Add(new CopyCommand());
commands.Add(new RequestsCommand()); commands.Add(new RequestsCommand());