public List <GuildUser> GetGuildUsers(ulong guildId) { SocketGuild guild = DiscordService.DiscordClient.GetGuild(guildId); if (guild == null) { throw new Exception("Unable to access guild"); } // Get the guild users IReadOnlyCollection <Discord.IGuildUser> guildUsers = guild.GetUsersAsync().ToEnumerable().FirstOrDefault(); // Get database users List <User> users = UserService.GetAllUsersForGuild(guildId).Result; List <GuildUser> results = new List <GuildUser>(); foreach (User guildUser in users) { string username = "******"; Discord.IGuildUser user = guildUsers.FirstOrDefault(x => x.Id == guildUser.DiscordUserId); if (user != null) { username = user.Nickname ?? user.Username; } results.Add(new GuildUser(guildUser.DiscordUserId, username, guildUser.TotalKupoNutsCurrent, guildUser.TotalXPCurrent, guildUser.Level, guildUser.Reputation)); } return(results); }
public async Task Kick(Discord.IGuildUser userAccount, string reason) { var user = Context.User as Discord.IGuildUser; var role = (user as Discord.IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == "Admin"); if (!userAccount.Guild.Roles.Contains(role)) { if (user.GuildPermissions.KickMembers) { await userAccount.KickAsync(reason); await Context.Channel.SendMessageAsync($"The user `{userAccount}` has been kicked, for {reason}"); } else { await Context.Channel.SendMessageAsync("No permissions for kicking a user."); } } else { await Context.Channel.SendMessageAsync("This User can't be kicked, because the user has a admin role."); } }
private string Remote(CultureInfo ci, StringWriter sw, string assembly, string args, Discord.IGuildUser sender, Discord.IGuild server, Discord.ITextChannel channel) { CultureInfo.DefaultThreadCurrentCulture = ci; CultureInfo.DefaultThreadCurrentUICulture = ci; Console.SetOut(sw); Console.SetError(sw); Assembly script = Assembly.LoadFrom(assembly); MethodInfo method = script.GetType("DuckBot.Script").GetMethod("Code"); Task <string> task = Task.Run(() => (string)method.Invoke(null, new object[] { args, sender, server, channel })); return(task.Wait(90000) ? task.GetAwaiter().GetResult() : throw new TargetInvocationException(null)); }
public static async Task Show(ICommandContext context, string key) { var ttag = await Program.P.Db.SendTag(key); if (ttag == null) { await context.Channel.SendMessageAsync("There is no tag with this name."); } else { if (ttag.Type == TagType.TEXT) { await context.Channel.SendMessageAsync((string)ttag.Content); } else if (ttag.Type == TagType.IMAGE) { using MemoryStream ms = new MemoryStream((byte[])ttag.Content); await context.Channel.SendFileAsync(ms, "Image" + ttag.Extension); } else if (ttag.Type == TagType.AUDIO) { Discord.IGuildUser guildUser = context.User as Discord.IGuildUser; if (guildUser.VoiceChannel == null) { await context.Channel.SendMessageAsync("You must be in a vocal channel for vocal tags."); } else { IAudioClient audioClient = await guildUser.VoiceChannel.ConnectAsync(); if (!File.Exists("ffmpeg.exe")) { throw new FileNotFoundException("ffmpeg.exe was not found near the bot executable."); } string vOutput = ""; // Download the file to play string fileName = "audio" + Program.P.Rand.Next(0, 1000000) + ttag.Extension; File.WriteAllBytes(fileName, (byte[])ttag.Content); // Get the current volume of the audio Process process = Process.Start(new ProcessStartInfo { FileName = "ffmpeg.exe", Arguments = $"-i {fileName} -filter:a volumedetect -f null /dev/null:", UseShellExecute = false, RedirectStandardError = true }); process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => { vOutput += e.Data; }; process.BeginErrorReadLine(); process.WaitForExit(); double volume = double.Parse(Regex.Match(vOutput, "mean_volume: ([-0-9.]+) dB").Groups[1].Value); // Set the new volume so it's neither too loud, neither not enough double objective = -30 - volume; // Add a delay if the audio is too short, if we don't the audio is somehow not played string delay = Regex.Match(vOutput, "Duration: 00:00:00").Success ? ",\"adelay=1000|1000\"" : ""; process = Process.Start(new ProcessStartInfo { FileName = "ffmpeg.exe", Arguments = $"-hide_banner -loglevel panic -i {fileName} -af volume={objective}dB{delay} -ac 2 -f s16le -ar 48000 pipe:", UseShellExecute = false, RedirectStandardOutput = true }); using Stream output = process.StandardOutput.BaseStream; using AudioOutStream discord = audioClient.CreatePCMStream(AudioApplication.Music); try { await output.CopyToAsync(discord); } catch (OperationCanceledException) { } await discord.FlushAsync(); await audioClient.StopAsync(); File.Delete(fileName); } } } }