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

View File

@ -124,7 +124,7 @@ namespace NadekoBot.Classes.Music {
$"**【 {SongInfo.Title.TrimTo(55)} 】**`{(SongInfo.Provider ?? "-")}`";
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;
public MusicPlayer MusicPlayer { get; set; }
@ -149,35 +149,38 @@ namespace NadekoBot.Classes.Music {
var buffer = new byte[blockSize];
var attempt = 0;
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 (attempt++ == 20)
break;
else
await Task.Delay(50, cancelToken);
await Task.Delay(40, cancelToken);
else
attempt = 0;
await songBuffer.WriteAsync(buffer, read, cancelToken);
if (songBuffer.ContentLength > 2.MB())
prebufferingComplete = true;
}
} catch {
Console.WriteLine("Buffering errored");
} catch (Exception ex) {
Console.WriteLine($"Buffering errored: {ex.Message}");
} finally {
Console.WriteLine($"Buffering done." + $" [{songBuffer.ContentLength}]");
if (p != null) {
try {
p.Kill();
p.WaitForExit();
}
catch {}
} catch { }
p.Dispose();
}
}
});
internal async Task Play(IAudioClient voiceClient, CancellationToken cancelToken) {
var bufferTask = BufferSong(cancelToken);
var bufferTask = BufferSong(cancelToken).ConfigureAwait(false);
var bufferAttempts = 0;
const int waitPerAttempt = 500;
var toAttemptTimes = SongInfo.ProviderType != MusicType.Normal ? 5 : 9;
@ -194,28 +197,33 @@ namespace NadekoBot.Classes.Music {
var read = songBuffer.Read(buffer, blockSize);
if (read == 0)
if (attempt++ == 20) {
Console.WriteLine("Nothing to read.");
voiceClient.Wait();
await bufferTask;
return;
} else
await Task.Delay(50);
}
else
await Task.Delay(50, cancelToken);
else
attempt = 0;
while (this.MusicPlayer.Paused)
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);
}
await bufferTask;
voiceClient.Clear();
cancelToken.ThrowIfCancellationRequested();
}
//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)
return audioSamples;
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
short buf1 = audioSamples[i + 1];

View File

@ -20,6 +20,8 @@ namespace NadekoBot.Modules {
commands.Add(new PlayingRotate());
}
public override string Prefix { get; } = ".";
public override void Install(ModuleManager manager) {
manager.CreateCommands("", cgb => {
@ -146,8 +148,7 @@ namespace NadekoBot.Modules {
await role.Edit(color: new Color(red, green, blue));
await e.Channel.SendMessage($"Role {role.Name}'s color has been changed.");
} catch (Exception ex) {
await e.Channel.SendMessage("Error occured, most likely invalid parameters.");
Console.WriteLine($".rolecolor error: {ex}");
await e.Channel.SendMessage("Error occured, most likely invalid parameters or insufficient permissions.");
}
});
@ -166,35 +167,56 @@ namespace NadekoBot.Modules {
});
cgb.CreateCommand(".b").Alias(".ban")
.Parameter("everything", ParameterType.Unparsed)
.Description("Bans a mentioned user.")
.Parameter("user", ParameterType.Required)
.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 => {
var msg = e.GetArg("msg");
var user = e.GetArg("user");
if (e.User.ServerPermissions.BanMembers) {
var usr = e.Server.FindUsers(user).FirstOrDefault();
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 {
if (e.User.ServerPermissions.BanMembers && e.Message.MentionedUsers.Any()) {
var usr = e.Message.MentionedUsers.First();
await usr.Server.Ban(usr);
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")
.Parameter("user")
.Parameter("msg",ParameterType.Unparsed)
.Description("Kicks a mentioned user.")
.Do(async e => {
try {
if (e.User.ServerPermissions.KickMembers && e.Message.MentionedUsers.Any()) {
var usr = e.Message.MentionedUsers.FirstOrDefault();
if (usr == null)
var msg = e.GetArg("msg");
var user = e.GetArg("user");
if (e.User.ServerPermissions.KickMembers) {
var usr = e.Server.FindUsers(user).FirstOrDefault();
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 e.Channel.SendMessage("Kicked user " + usr.Name + " Id: " + usr.Id);
}
} catch {
await e.Channel.SendMessage("No sufficient permissions.");
await e.Channel.SendMessage("Error. Most likely I don't have sufficient permissions.");
}
}
});
cgb.CreateCommand(".mute")

View File

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