private void StreamersEdit_Click(object sender, RoutedEventArgs e) { if (streamerListBox.SelectedIndex == -1) { return; } StreamerEditor se = new StreamerEditor(); se.Owner = this; se._streamer = _server.StreamerList[streamerListBox.SelectedIndex]; se.Title = $"Edit Streamer {se._streamer.DiscordName}"; IsEnabled = false; se.ShowDialog(); IsEnabled = true; if (se.DialogResult.HasValue && se.DialogResult.Value) { se._streamer.DiscordId = ulong.Parse(se.discordIdBox.Text); se._streamer.DiscordName = _client.Guilds.FirstOrDefault(x => x.Id == _server.ServerId).Users.FirstOrDefault(y => y.Id == ulong.Parse(se.discordIdBox.Text)).ToString(); se._streamer.TwitchName = se.twitchNameBox.Text; se._streamer.Mention = (MentionLevel)se.mentionLevelComboBox.SelectedItem; se._streamer.GiveRole = se.giveRoleCheckBox.IsChecked.Value; se._streamer.AutoPost = se.autoPostCheckBox.IsChecked.Value; UpdateView("streamers"); BotFrame.SaveFile("servers"); } }
private async Task JoinedGuildHandler(SocketGuild guild) { DiscordServer saved = _main.ServerList.FirstOrDefault(x => x.ServerId == guild.Id); if (saved != null) { saved.Active = true; } else { DiscordServer newServer = new DiscordServer() { Active = true, ServerName = guild.Name, ServerId = guild.Id, Prefix = PrefixChar.None, ServerJoined = DateTime.Now.ToString() }; App.Current.Dispatcher.Invoke((Action) delegate { _main.ServerList.Add(newServer); }); } BotFrame.SaveFile("servers"); _main.ServerList.Clear(); BotFrame.LoadFile("servers"); }
private void ReactionLocksEdit_Click(object sender, RoutedEventArgs e) { if (reactionListBox.SelectedIndex == -1) { return; } ReactionLockEditor rl = new ReactionLockEditor(); rl.Owner = this; rl._lock = _server.ReactionLockList[reactionListBox.SelectedIndex]; rl.Title = $"Edit lock {rl._lock.MessageId}"; IsEnabled = false; rl.ShowDialog(); IsEnabled = true; if (rl.DialogResult.HasValue && rl.DialogResult.Value) { rl._lock.ChannelId = ulong.Parse(rl.channelBox.Text); rl._lock.MessageId = ulong.Parse(rl.messageBox.Text); rl._lock.Emote = rl.emoteBox.Text; rl._lock.GiveRole = ulong.Parse(rl.giveRoleBox.Text); rl._lock.TakeRole = ulong.Parse(rl.takeRoleBox.Text); UpdateView("reactionlock"); BotFrame.SaveFile("servers"); } }
//ReactionLocks private void ReactionLocksAdd_Click(object sender, RoutedEventArgs e) { ReactionLockEditor rl = new ReactionLockEditor(); rl.Owner = this; rl.Title = "New Reaction Lock"; IsEnabled = false; rl.ShowDialog(); IsEnabled = true; if (rl.DialogResult.HasValue && rl.DialogResult.Value) { if (_server.ReactionLockList.FirstOrDefault(x => x.ChannelId == ulong.Parse(rl.channelBox.Text) && x.MessageId == ulong.Parse(rl.messageBox.Text)) != null) { MessageBox.Show("You are trying to add a reaction lock that already has an entry, please select the reaction lock entry and edit it instead!", "Error!", MessageBoxButton.OK, MessageBoxImage.Error); return; } ReactionLock newLock = new ReactionLock() { ChannelId = ulong.Parse(rl.channelBox.Text), MessageId = ulong.Parse(rl.messageBox.Text), Emote = rl.emoteBox.Text, GiveRole = ulong.Parse(rl.giveRoleBox.Text), TakeRole = ulong.Parse(rl.takeRoleBox.Text) }; if (_server.ReactionLockList == null) { _server.ReactionLockList = new List <ReactionLock>(); } _server.ReactionLockList.Add(newLock); UpdateView("reactionlock"); BotFrame.SaveFile("servers"); } }
private async Task UserJoinedHandler(SocketGuildUser u) { if (u == null) { return; } var server = _main.ServerList.FirstOrDefault(x => x.ServerId == u.Guild.Id); if (u.IsBot) { return; } if (server.NewUserRole != 0) { try { await u.AddRoleAsync(u.Guild.Roles.FirstOrDefault(x => x.Id == server.NewUserRole)); } catch (Exception ex) { BotFrame.consoleOut(ex.Message); return; } } }
public static async Task SaveFile(string file) { consoleOut($"Saving {file}..."); string json = ""; switch (file) { case "config": json = JsonConvert.SerializeObject(config, Formatting.Indented); break; case "servers": json = JsonConvert.SerializeObject(_main.ServerList, Formatting.Indented); break; } try { File.WriteAllText($"{resDir}/{file}.json", json); } catch (Exception ex) { BotFrame.consoleOut(ex.Message); return; } consoleOut($"Saved {file}!"); }
private async Task UserLeaveHandler(SocketGuildUser u) { var server = _main.ServerList.FirstOrDefault(x => x.ServerId == u.Guild.Id); var streamer = server.StreamerList.FirstOrDefault(x => x.DiscordId == u.Id); if (streamer != null) { server.StreamerList.Remove(streamer); BotFrame.SaveFile("servers"); } }
//Global private void Save_Click(object sender, RoutedEventArgs e) { //Save //Streamers //ReactionLocks //Settings if (serverPrefixCheckBox.IsChecked == true) { _server.Prefix = (PrefixChar)serverPrefixBox.SelectedItem; } if (ulong.TryParse(adminRoleBox.Text, out ulong adminRole)) { _server.AdminRole = adminRole; } else { _server.AdminRole = 0; } if (ulong.TryParse(botChanBox.Text, out ulong botChan)) { _server.BotChannel = botChan; } else { _server.BotChannel = 0; } if (ulong.TryParse(newUserRoleBox.Text, out ulong newUserRole)) { _server.NewUserRole = newUserRole; } else { _server.NewUserRole = 0; } _server.StreamerCheckInterval = intervalSlider.Value; if (ulong.TryParse(streamPostChannelBox.Text, out ulong postChannel)) { _server.StreamPostChannel = postChannel; } else { _server.StreamPostChannel = 0; } if (ulong.TryParse(streamerRoleBox.Text, out ulong streamerRole)) { _server.StreamingRole = streamerRole; } else { _server.StreamingRole = 0; } BotFrame.SaveFile("servers"); }
private async Task MessageReceivedHandler(SocketMessage msg) { SocketUserMessage message = msg as SocketUserMessage; if (message == null) { return; } SocketCommandContext context = new SocketCommandContext(_client, message); if (context.User.IsBot) { return; } var server = _main.ServerList.FirstOrDefault(x => x.ServerId == context.Guild.Id); bool admin = false; var hasAdminRole = context.Guild.Users.FirstOrDefault(x => x.Id == context.User.Id).Roles.FirstOrDefault(y => y.Id == server.AdminRole); if (hasAdminRole != null && hasAdminRole.Id == server.AdminRole) { admin = true; } int argPos = 0; if (message.HasStringPrefix(((Char)server.Prefix).ToString(), ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos)) { if ((server.BotChannel != 0 && msg.Channel.Id == server.BotChannel) || server.BotChannel == 0 || admin) { var res = await _service.ExecuteAsync(context, argPos, null); if (!res.IsSuccess && res.Error != CommandError.UnknownCommand) { BotFrame.consoleOut($"Command Handler Error: {res.ErrorReason}"); return; } if (!res.IsSuccess && res.Error == CommandError.UnknownCommand) { await Task.Delay(200); await message.DeleteAsync(); BotFrame.consoleOut($"Unknown command! Deleted message from user {context.User} in channel {context.Channel.Name}{Environment.NewLine}Message: {message.Content}"); return; } } else { BotFrame.consoleOut($"Delete message from user {context.User} in channel {context.Channel.Name}{Environment.NewLine}Message: {context.Message.Content}"); return; } } }
public async Task StreamerHandler(DiscordServer server) { server.StreamerCheckElapsed = 0; foreach (var streamer in server.StreamerList) { var guild = _client.Guilds.FirstOrDefault(x => x.Id == server.ServerId); var streamRole = guild.Roles.FirstOrDefault(x => x.Id == server.StreamingRole); var channel = guild.Channels.FirstOrDefault(x => x.Id == server.StreamPostChannel) as ISocketMessageChannel; var user = guild.Users.FirstOrDefault(x => x.Id == streamer.DiscordId); var stream = await _api.V5.Streams.GetStreamByUserAsync((await _api.V5.Users.GetUserByNameAsync(streamer.TwitchName)).Matches[0].Id); if (stream.Stream == null)//not streaming { if (streamer.Streaming) { streamer.Game = ""; streamer.Streaming = false; if (server.StreamingRole != 0 && streamer.GiveRole && user.Roles.Contains(streamRole)) { await user.RemoveRoleAsync(streamRole); } BotFrame.SaveFile("servers"); } continue; } if (!streamer.Streaming)//not marked { streamer.Streaming = true; if (server.StreamingRole != 0 && streamer.GiveRole && !user.Roles.Contains(streamRole)) { await user.AddRoleAsync(streamRole); } } if (stream.Stream.Channel.Game != streamer.Game) { streamer.Game = stream.Stream.Channel.Game; if (server.StreamPostChannel != 0 && streamer.AutoPost) { BotFrame.StreamPost(channel, user, stream.Stream, (int)streamer.Mention); } BotFrame.SaveFile("servers"); } } }
private void ReactionLocksDelete_Click(object sender, RoutedEventArgs e) { if (reactionListBox.SelectedIndex == -1) { return; } IsEnabled = false; var res = MessageBox.Show("Are you sure you want to remove this item?", "Delete?", MessageBoxButton.YesNo, MessageBoxImage.Question); IsEnabled = true; if (res == MessageBoxResult.No) { return; } _server.ReactionLockList.Remove(_server.ReactionLockList[reactionListBox.SelectedIndex]); UpdateView("reactionlock"); BotFrame.SaveFile("servers"); }
private async Task ServerCheck() { if (_client == null) { return; } foreach (var connectedGuild in _client.Guilds) { DiscordServer saved = ServerList.FirstOrDefault(x => x.ServerId == connectedGuild.Id); if (saved != null) { if (!saved.Active) { saved.Active = true; } } else { DiscordServer newServer = new DiscordServer() { Active = true, ServerName = connectedGuild.Name, ServerId = connectedGuild.Id, Prefix = PrefixChar.None, ServerJoined = DateTime.Now.ToString() }; ServerList.Add(newServer); } } foreach (var savedGuild in ServerList.Where(x => x.Active)) { var check = _client.Guilds.FirstOrDefault(x => x.Id == savedGuild.ServerId); if (check == null) { savedGuild.Active = false; } } BotFrame.SaveFile("servers"); ServerList.Clear(); BotFrame.LoadFile("servers"); }
private void ConsoleToken_Click(object sender, RoutedEventArgs e) { ConsoleToken tp = new ConsoleToken(); if (BotFrame.config == null) { BotFrame.config = new Config(); } if (BotFrame.config.DiscordToken != null) { tp.discordTokenBox.Text = BotFrame.config.DiscordToken; } if (BotFrame.config.TwitchToken != null) { tp.twitchTokenBox.Text = BotFrame.config.TwitchToken; } if (BotFrame.config.TwitchClientId != null) { tp.twitchClientIdBox.Text = BotFrame.config.TwitchClientId; } tp.Owner = this; IsEnabled = false; tp.ShowDialog(); IsEnabled = true; if (tp.DialogResult.HasValue && tp.DialogResult.Value) { BotFrame.config.DiscordToken = tp.discordTokenBox.Text; BotFrame.config.TwitchToken = tp.twitchTokenBox.Text; BotFrame.config.TwitchClientId = tp.twitchClientIdBox.Text; BotFrame.SaveFile("config"); connectionButton.IsEnabled = false; if (BotFrame.config.DiscordToken != null || BotFrame.config.DiscordToken != "") { connectionButton.IsEnabled = true; } } }
//Streamers private void StreamersAdd_Click(object sender, RoutedEventArgs e) { StreamerEditor se = new StreamerEditor(); se.Owner = this; se.Title = "New Streamer"; se.mentionLevelComboBox.SelectedItem = MentionLevel.None; IsEnabled = false; se.ShowDialog(); IsEnabled = true; if (se.DialogResult.HasValue && se.DialogResult.Value) { if (_server.StreamerList.FirstOrDefault(x => x.DiscordId == ulong.Parse(se.discordIdBox.Text) || x.TwitchName == se.twitchNameBox.Text) != null) { MessageBox.Show("You are trying to add a user that already has an entry, please select the user entry and edit it instead!", "Error!", MessageBoxButton.OK, MessageBoxImage.Error); return; } Streamer newStreamer = new Streamer() { Streaming = false, DiscordId = ulong.Parse(se.discordIdBox.Text), DiscordName = _client.Guilds.FirstOrDefault(x => x.Id == _server.ServerId).Users.FirstOrDefault(y => y.Id == ulong.Parse(se.discordIdBox.Text)).ToString(), Mention = (MentionLevel)se.mentionLevelComboBox.SelectedItem, GiveRole = se.giveRoleCheckBox.IsChecked.Value, AutoPost = se.autoPostCheckBox.IsChecked.Value, TwitchName = se.twitchNameBox.Text }; if (_server.StreamerList == null) { _server.StreamerList = new List <Streamer>(); } _server.StreamerList.Add(newStreamer); UpdateView("streamer"); BotFrame.SaveFile("servers"); } }
//Init public MainWindow() { InitializeComponent(); DataContext = this; CommandHandler._main = this; Modules.Signup._main = this; BotFrame._main = this; BotFrame.LoadFile("config"); BotFrame.LoadFile("servers"); if (BotFrame.config == null) { BotFrame.config = new Config(); } if (BotFrame.config.DiscordToken != null || BotFrame.config.DiscordToken == "") { connectionButton.IsEnabled = true; } if (ServerList == null) { ServerList = new ObservableCollection <DiscordServer>(); } _api = new TwitchAPI(); _timer.Elapsed += StreamerCheck_Elapsed; }
private async Task ReactionAddedHandler(Discord.Cacheable <Discord.IUserMessage, ulong> userMessage, ISocketMessageChannel messageChannel, SocketReaction reaction) { SocketGuildChannel guildChannel = messageChannel as SocketGuildChannel; SocketGuildUser user = guildChannel.Guild.Users.FirstOrDefault(x => x.Id == reaction.UserId); DiscordServer server = _main.ServerList.FirstOrDefault(x => x.ServerId == guildChannel.Guild.Id); if (server == null || user == null) { return; } //Reaction Locks foreach (var reactionLock in server.ReactionLockList.Where(x => x.ChannelId == guildChannel.Id && x.MessageId == userMessage.Id)) { if (reactionLock.Emote == reaction.Emote.ToString()) { var giveRole = guildChannel.Guild.Roles.FirstOrDefault(x => x.Id == reactionLock.GiveRole); var takeRole = guildChannel.Guild.Roles.FirstOrDefault(x => x.Id == reactionLock.TakeRole); if (giveRole == null) { return; } if (!user.Roles.Contains(giveRole)) { BotFrame.consoleOut($"Adding role @{giveRole} to user {user} in server {guildChannel.Guild.Name}"); await user.AddRoleAsync(giveRole); if (takeRole != null) { BotFrame.consoleOut($"Removing role @{takeRole} from user {user} in server {guildChannel.Guild.Name}"); await user.RemoveRoleAsync(takeRole); } } } } //-------------- }
public async Task Log(LogMessage msg) { BotFrame.consoleOut(msg.Message); }
private void ServerSave_Click(object sender, RoutedEventArgs e) { BotFrame.SaveFile("servers"); }
private void ServerLoad_Click(object sender, RoutedEventArgs e) { BotFrame.LoadFile("servers"); }
//Server Manager private void ServerRefresh_Click(object sender, RoutedEventArgs e) { ServerList.Clear(); BotFrame.LoadFile("servers"); }
private void ConsoleClear_Click(object sender, RoutedEventArgs e) { BotFrame.consoleClear(); }