Esempio n. 1
0
 private string PhraseToString(Phrase phrase)
 {
     return($"Trigger: {phrase.triggerPhrase}, " +
            "User: "******"Any" : GuildHandler.GetUser(phrase.userID).GetShownName()) + ", " +
            "Channel: " + (GuildHandler.GetChannel(phrase.channelID) == null ? "Any" : GuildHandler.GetChannel(phrase.channelID).Name) + ", " +
            $"Chance: {Math.Round (phrase.chance, 2)}%, Response: {phrase.response}, Emote: {phrase.emoji}");
 }
Esempio n. 2
0
        private async Task SendMessage(SocketGuild guild, CachedValue <List <string> > messages, params string[] findAndReplace)
        {
            SocketTextChannel channel = GuildHandler.GetChannel(_channelId.GetValue()) as SocketTextChannel;

            string [] guildMessages = messages.GetValue().ToArray();
            string    message       = guildMessages [new Random().Next(0, guildMessages.Length)];

            for (int i = 0; i < findAndReplace.Length; i += 2)
            {
                message = message.Replace(findAndReplace[i], findAndReplace[i + 1]);
            }

            await MessageControl.SendMessage(channel, message);
        }
Esempio n. 3
0
        public override void Initialize()
        {
            AssertPermission(GuildPermission.ManageChannels);

            _commandSet = new VoiceNameSet {
                ParentPlugin = this
            };
            GuildHandler.ChannelCreated        += OnChannelCreated;
            GuildHandler.ChannelDestroyed      += OnChannelDestroyed;
            GuildHandler.UserVoiceStateUpdated += OnVoiceStateUpdated;
            GuildHandler.GuildMemberUpdated    += OnGuildMemberUpdated;
            GuildHandler.ChannelUpdated        += OnChannelUpdated;
            InitDefaultTags();

            _channelNames = GetConfigCache("ChannelNames", x => x.GetGuild().VoiceChannels.ToDictionary(y => y.Id, z => z.Name));
            _channelNames.Store();

            _toIgnore = GetConfigCache("ToIgnore", x => new List <ulong> {
                (x.GetGuild().AFKChannel?.Id).GetValueOrDefault()
            });
            _musicBotId          = GetConfigCache("MusicBotId", x => (ulong)0);
            _internationalRoleId = GetConfigCache("MusicBotId", x => (ulong)0);
            _nameFormat          = GetConfigCache("NameFormat", x => $"{_formatNameStr} - {_formatGameStr} ({_formatAmountPlayersStr})");

            AddConfigInfo("Set Channel Name", "Display channel names", () => "Current channel names:\n" + string.Join('\n', _channelNames.GetValue().Select(x => x.Value).ToArray()));
            AddConfigInfo <SocketVoiceChannel, string>("Set Channel Name", "Set channel name", (x, y) => _channelNames.MutateValue(z => z[x.Id] = y), (x, y) => $"Succesfully set channel '{x.Name}' to '{y}'", "Channel", "New name");
            AddConfigInfo <string, string>("Set Channel Name", "Set channel name", (x, y) => _channelNames.MutateValue(z => z[GuildHandler.GetVoiceChannel(x).Id] = y), (x, y) => "Succesfully set channel names.", "Channel", "New name");

            AddConfigInfo <SocketVoiceChannel>("Dont Name Channel", "Ignore channel", x => _toIgnore.MutateValue(y => y.Add(x.Id)), x => $"Added channel '{x.Name}' to list of ignored channels.", "Channel");
            AddConfigInfo <ulong>("Dont Name Channel", "Ignore channel", x => _toIgnore.MutateValue(y => y.Add(GuildHandler.GetVoiceChannel(x).Id)), x => $"Added channel '{GuildHandler.GetVoiceChannel (x).Name}' to list of ignored channels.", "ignored");
            AddConfigInfo <string>("Dont Name Channel", "Ignore channel", x => _toIgnore.MutateValue(y => y.Add(GuildHandler.GetVoiceChannel(x).Id)), x => $"Added channel '{GuildHandler.GetVoiceChannel(x).Name}' to list of ignored channels.", "Channel");

            AddConfigInfo <SocketVoiceChannel>("Do Name Channel", "Unignore channel", x => _toIgnore.MutateValue(y => y.Remove(x.Id)), x => $"Removed channel '{x.Name}' from list of ignored.", "Channel");
            AddConfigInfo <ulong>("Do Name Channel", "Unignore channel", x => _toIgnore.MutateValue(y => y.Remove(GuildHandler.GetVoiceChannel(x).Id)), x => $"Removed channel '{GuildHandler.GetChannel(x)}' from list of ignored.", "Channel");
            AddConfigInfo <string>("Do Name Channel", "Unignore channel", x => _toIgnore.MutateValue(y => y.Remove(GuildHandler.GetVoiceChannel(x).Id)), x => $"Removed channel '{GuildHandler.GetChannel(x)}' from list of ignored.", "Channel");

            AddConfigInfo <SocketGuildUser>("Set Music Bot", "Set music bot.", x => _musicBotId.SetValue(x.Id), x => $"Set music bot to be {x.Id}.", "Music Bot");
            AddConfigInfo <string>("Set Music Bot", "Set music bot.", x => _musicBotId.SetValue(GuildHandler.GetUser(x).Id), x => $"Set music bot to be {GuildHandler.GetUser(x).GetShownName()}.", "Music Bot");
            AddConfigInfo("Set Music Bot", "Show music bot.", () => GuildHandler.FindUser(_musicBotId.GetValue()) == null ? "Current music bot doesn't exist :(" : "Current music bot is " + GuildHandler.GetUser(_musicBotId.GetValue()).GetShownName());

            AddConfigInfo <SocketRole>("Set International Role", "Set role.", x => _internationalRoleId.SetValue(x.Id), x => $"Set international role to be {x}.", "Role");
            AddConfigInfo <string>("Set International Role", "Set role.", x => _internationalRoleId.SetValue(GuildHandler.GetRole(x).Id), x => $"Set international role to be {GuildHandler.GetRole(x).Name}.", "Role Name");
            AddConfigInfo("Set International Role", "Show role.", () => GuildHandler.FindRole(_internationalRoleId.GetValue()) == null ? "Current international role doesn't exist :(" : "Current international role is " + GuildHandler.GetRole(_internationalRoleId.GetValue()).Name);

            AddConfigInfo("Set Name Format", "Set format", () => $"Current format is '{_nameFormat.GetValue()}' which might look like this in practice: '{FormatName(_nameFormat.GetValue(), "General 1", "Cool Game 3: The Coolest", 5)}'.");
            AddConfigInfo <string>("Set Name Format", "Set format", x => _nameFormat.SetValue(x), x => $"Set format to '{x}' which might look like this in practice: '{FormatName(x, "General 1", "Cool Game 3: The Coolest", 5)}'.", "Format");
            SendMessage("Moduthulhu-Command Root", "AddCommand", _commandSet);

            AddGeneralFeaturesStateAttribute("AutomatedVoiceNames", "Automatically changing voice channel names to reflect games played within.");

            RegisterMessageAction("AddTag", x => AddTag(new Tag((string)x[0], (string)x[1], (Func <SocketVoiceChannel, bool>)x[2])));
            RegisterMessageAction("RemoveTag", x => RemoveTag((string)x[0]));

            SetStateChangeHeaders("Tags", "The following voice channel tags has been added", "The following voice channel tags has been removed", "The following  voice channel tags has been modified");

            RegisterMessageAction("UpdateChannel", x => UpdateChannel(GuildHandler.GetVoiceChannel((ulong)x[0])).ConfigureAwait(false));
        }
Esempio n. 4
0
        public async Task UpdateChannel(SocketVoiceChannel channel)
        {
            string highestGame = "";

            DisablePluginIfPermissionMissing(GuildPermission.ManageChannels, true);

            if (channel != null)
            {
                string name = _channelNames.GetValue().GetValueOrDefault(channel.Id);

                if (GuildHandler.GetChannel(channel.Id) == null)
                {
                    return;
                }

                if (_toIgnore.GetValue().Contains(channel.Id))
                {
                    return;
                }

                if (string.IsNullOrEmpty(name))    // If the channel is unknown, then add it and retry through OnChannelCreated.
                {
                    await OnChannelCreated(channel);

                    return;
                }

                List <SocketGuildUser> users = channel.Users.ToList();

                Dictionary <string, int> numPlayers = new Dictionary <string, int> ();
                foreach (SocketGuildUser user in users)
                {
                    if (user.Activity == null)
                    {
                        continue;
                    }

                    int unknowns = 0;
                    if (!user.IsBot)
                    {
                        if (user.Activity.Type == ActivityType.Playing)
                        {
                            string activity = user.Activity.ToString();

                            if (!numPlayers.ContainsKey(activity))
                            {
                                numPlayers.Add(activity, 0);
                            }
                            numPlayers[activity]++;
                        }
                        else
                        {
                            unknowns++;
                        }
                    }

                    // Temporary solution to the bot not being able to see game being played, if the user has a custom status.
                    // In this case, if the user is not a bot, it is assumed they are playing all games currently being played by others.
                    for (int i = 0; i < numPlayers.Count; i++)
                    {
                        numPlayers[numPlayers.ElementAt(i).Key] += unknowns;
                    }
                }

                int highest = int.MinValue;

                for (int i = 0; i < numPlayers.Count; i++)
                {
                    KeyValuePair <string, int> value = numPlayers.ElementAt(i);

                    if (value.Value > highest)
                    {
                        highest     = value.Value;
                        highestGame = value.Key;
                    }
                }

                string [] splitVoice      = name.Split(':');
                string    possibleShorten = splitVoice.Length > 1 ? splitVoice [1] : splitVoice [0];

                string tags    = GetTags(channel);
                string newName = splitVoice[0];
                if (!string.IsNullOrWhiteSpace(highestGame))
                {
                    newName = FormatName(_nameFormat.GetValue(), possibleShorten, highestGame, highest);
                }
                newName = tags + " " + newName;

                if (channel.Users.Count == 0)
                {
                    _customNames.Remove(channel.Id);
                }
                if (_customNames.ContainsKey(channel.Id))
                {
                    newName = FormatName(_nameFormat.GetValue(), possibleShorten, _customNames[channel.Id], 0);
                }

                // Trying to optimize API calls here, just to spare those poor souls at the Discord API HQ stuff
                if (channel.Name != newName)
                {
                    try {
                        _pendingNameChanges.Add(channel.Id, newName);
                        await channel.ModifyAsync(x => x.Name = newName);
                    }catch (Exception e) {
                        Core.Log.Exception(e);
                    }
                }
            }
        }
Esempio n. 5
0
        public async Task UpdateChannel(SocketVoiceChannel channel)
        {
            string highestGame = "";

            if (channel != null)
            {
                string name = _channelNames.GetValue().GetValueOrDefault(channel.Id);

                if (GuildHandler.GetChannel(channel.Id) == null)
                {
                    return;
                }

                if (_toIgnore.GetValue().Contains(channel.Id))
                {
                    return;
                }

                if (string.IsNullOrEmpty(name))    // If the channel is unknown, then add it and retry through OnChannelCreated.
                {
                    await OnChannelCreated(channel);

                    return;
                }

                List <SocketGuildUser> users = channel.Users.ToList();

                Dictionary <string, int> numPlayers = new Dictionary <string, int> ();
                foreach (SocketGuildUser user in users)
                {
                    if (user.Activity == null)
                    {
                        continue;
                    }

                    if (user.Activity.Type == ActivityType.Playing && user.IsBot == false)
                    {
                        if (numPlayers.ContainsKey(user.Activity.Name))
                        {
                            numPlayers [user.Activity.Name]++;
                        }
                        else
                        {
                            numPlayers.Add(user.Activity.Name, 1);
                        }
                    }
                }

                int highest = int.MinValue;

                for (int i = 0; i < numPlayers.Count; i++)
                {
                    KeyValuePair <string, int> value = numPlayers.ElementAt(i);

                    if (value.Value > highest)
                    {
                        highest     = value.Value;
                        highestGame = value.Key;
                    }
                }

                string [] splitVoice      = name.Split(':');
                string    possibleShorten = splitVoice.Length > 1 ? splitVoice [1] : splitVoice [0];

                string tags    = GetTags(channel);
                string newName = highestGame != "" ? possibleShorten + " - " + highestGame : splitVoice [0];
                newName = tags + " " + newName;

                if (channel.Users.Count == 0)
                {
                    _customNames.Remove(channel.Id);
                }
                if (_customNames.ContainsKey(channel.Id))
                {
                    newName = possibleShorten + " - " + _customNames [channel.Id];
                }

                // Trying to optimize API calls here, just to spare those poor souls at the Discord API HQ stuff
                if (channel.Name != newName)
                {
                    try {
                        await channel.ModifyAsync(x => x.Name = newName);
                    }catch (Exception e) {
                        Core.Log.Write(e);
                    }
                }
            }
        }
Esempio n. 6
0
        private SocketVoiceChannel FindEmptyTemporaryChannel()
        {
            var temps = _temporaryChannels.Select(x => GuildHandler.GetChannel(x) as SocketVoiceChannel);

            return(temps.LastOrDefault(x => x.Users.Count == 0));
        }