Beispiel #1
0
        private static string CheckForMentions(IMessageChannel channel, string message)
        {
            var words = message.Split().Where(x => x.StartsWith("@"));

            if (!words.Any())
            {
                return(message);
            }

            var users = AsyncEnumerableExtensions.Flatten(channel.GetUsersAsync());

            users.Select(x => $"@{x.Username}").Intersect(words.ToAsyncEnumerable()).ForEach(async x =>
            {
                var user = await users.First(y => y.Username == x.Substring(1));
                message  = message.Replace(x, user.Mention);
            });

            if (channel is SocketTextChannel)
            {
                var castChannel = (SocketTextChannel)channel;
                var roles       = castChannel.Guild.Roles;
                roles.Select(x => $"@{x.Name}").Intersect(words).ToList().ForEach(x =>
                {
                    var role = roles.First(y => y.Name == x.Substring(1));
                    message  = message.Replace(x, role.Mention);
                });
            }

            return(message);
        }
Beispiel #2
0
        public static async Task <IUser> GetUserByUsername(string username, IMessageChannel channel)
        {
            var users = (await channel.GetUsersAsync().FlattenAsync()).Where(user => user.Username == username).ToArray();

            if (users.Length == 1)
            {
                return(users[0]);
            }
            if (users.Length > 0)
            {
                throw new ArgumentException($"Multiple users were found with the username {username}");
            }
            throw new ArgumentException($"No user was found with the username {username} in this channel");
        }
Beispiel #3
0
        private static string CheckForMentions(IMessageChannel channel, string message)
        {
            var words = message.Split().Where(x => x.StartsWith("@"));

            if (!words.Any())
            {
                return(message);
            }

            var users = AsyncEnumerableExtensions.Flatten(channel.GetUsersAsync());

            users.Select(x => $"@{x.Username}").Intersect(words.ToAsyncEnumerable()).ForEach(async x =>
            {
                var user = await users.First(y => y.Username == x.Substring(1));
                message  = message.Replace(x, user.Mention);
            });

            return(message);
        }
Beispiel #4
0
 public static IUser ParseUser(string userText, IMessageChannel c)
 {
     return(ParseUser(userText, c.GetUsersAsync().FlattenAsync().Result));
 }
Beispiel #5
0
        public async Task Purge([Remainder] string purgecommands)
        {
            try
            {
                var guser = Context.User as IGuildUser;
                if (!guser.GuildPermissions.Has(GuildPermission.ManageMessages))
                {
                    await Context.User.SendMessageAsync($"You do not have permissions to purge messages in {Context.Guild.Name}. Contact an admin if you wish to have messages removed!");

                    return;
                }
                var    guildId = (null == Context.Guild ? Context.Channel.Id : Context.Guild.Id);
                var    channel = Context.Channel;
                string user, count, purgeChannel;
                purgeChannel = count = user = string.Empty;
                //Parse arg string
                var commands = purgecommands.Split(' ');
                if (commands.Count() < 2 || commands.Count() > 3)
                {
                    await channel.SendMessageAsync("Usage: !purge user number [channel]");

                    return;
                }
                if (commands.Count() >= 2)
                {
                    user  = commands[0].ToLower();
                    count = commands[1].ToLower();
                }
                if (commands.Count() == 3)
                {
                    purgeChannel = commands[2].ToLower();
                }

                //Get channel
                IMessageChannel channel2purge = channel;
                if (!string.IsNullOrEmpty(purgeChannel))
                {
                    var textchannels = Context.Guild.GetTextChannelsAsync().Result;
                    channel2purge = textchannels.First(x => x.Name.ToLower().Contains(purgeChannel));
                }

                //Get user
                IUser user2purge = null;
                var   users      = channel2purge.GetUsersAsync().FirstOrDefault().Result;
                user2purge = users.First(x => (0 == string.Compare(x.Username, user, true)));
                foreach (IUser u in users)
                {
                    if (0 == string.Compare(u.Username.ToLower(), user))
                    {
                        user2purge = u;
                        break;
                    }
                }

                //Get count
                int number2purge = Int32.Parse(count);
                //Get messages;
                var msgs       = channel2purge.GetMessagesAsync(1000).Flatten();
                var usermsgs   = msgs.Where(x => x.Author == user2purge);
                var msgs2purge = usermsgs.Take(number2purge);
                var enumerator = msgs2purge.GetEnumerator();
                while (await enumerator.MoveNext())
                {
                    await channel2purge.DeleteMessageAsync(enumerator.Current);
                }
            }
            catch { }
        }
Beispiel #6
0
        private async void ListViewItem_Loaded(object sender, RoutedEventArgs e)
        {
            if (Channel is IGroupChannel)
            {
                IEnumerable <IUser> users = await Channel.GetUsersAsync().Flatten();

                channelName.Text = Channel.Name ?? string.Join(", ", users.Select(u => u.Username));

                groupIcon.Visibility    = Visibility.Visible;
                channelDescription.Text = users.Count() + " users";

                ContextMenu cm      = new ContextMenu();
                MenuItem    closeDM = new MenuItem();
                closeDM.Header = "Leave group";
                closeDM.Click += CloseDM_Click;
                cm.Items.Add(closeDM);
                ContextMenu = cm;

                App.DiscordWindow.Client.ChannelDestroyed += Client_ChannelDestroyed;
            }
            else
            {
                IDMChannel channel = Channel as IDMChannel;

                channelName.Text = channel.Recipient.Username;

                channelIconContainer.Visibility = Visibility.Visible;

                string url = channel.Recipient.GetAvatarUrl();
                if (url != null)
                {
                    channelIcon.ImageSource = Images.GetImage(url);
                }
                else
                {
                    channelIcon.ImageSource = App.Current.Resources["StockPFP"] as BitmapImage;
                }

                channelDescription.Text = channel.Recipient.Status.ToString();

                ContextMenu cm      = new ContextMenu();
                MenuItem    closeDM = new MenuItem();
                closeDM.Header = "Close DM";
                closeDM.Click += CloseDM_Click;
                cm.Items.Add(closeDM);
                ContextMenu = cm;

                if (channel.Recipient.Status == UserStatus.Online)
                {
                    channelDescription.Foreground = Brushes.LimeGreen;
                }
                else if (channel.Recipient.Status == UserStatus.Idle || channel.Recipient.Status == UserStatus.AFK)
                {
                    channelDescription.Foreground = Brushes.Orange;
                }
                else if (channel.Recipient.Status == UserStatus.DoNotDisturb)
                {
                    channelDescription.Foreground = Brushes.Red;
                }

                App.DiscordWindow.Client.UserUpdated      += Client_UserUpdated;
                App.DiscordWindow.Client.ChannelDestroyed += Client_ChannelDestroyed;
            }
        }