using axXez.Extensions; using axXez.TS3.Protocol; using System.Collections.Generic; using System.Linq; namespace axXez.TS3.Bot.Modules { [Module(Namespace = "axXez")] internal class Greetings : Module { public bool GreetAdmins { get; set; } = true; public bool GreetClients { get; set; } = false; public string Channel { get; set; } = null; public string Message { get; set; } = "[B]Greetings sir![/B]\n\n[COLOR=red]Use '[B]!help[/B]' to get a list of available commands.[/COLOR]"; public bool GreetAlreadyConnectedClients { get; set; } = false; public bool GreetClientsInEveryChannel { get; set; } = false; public List IgnoredUsers { get; set; } = new(); private int _channelId = -1; private string _adminWelcomeMessage; protected override void OnStart() { _channelId = Channel != null && Server.FindChannel(Channel, out var ch) ? ch.ID : -1; var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; _adminWelcomeMessage = $"[B]axXezBot v{version}[/B] online — {Bot.ActiveBotModules.Count} modules active | {Server.Binary.Version} ({Server.Binary.Platform}) | use [B]!help[/B] for commands"; if (GreetAdmins) { foreach (Client admin in Bot.GetConnectedAdmins()) Query.SendTextMessage(MessageTargetMode.Private, admin.ID, _adminWelcomeMessage); } if (!GreetClients || !GreetAlreadyConnectedClients) return; if (GreetClientsInEveryChannel) { Clients.Values.Where(c => !IgnoredUsers.Contains(c.UID)).ForEach(c => Query.SendTextMessage(MessageTargetMode.Private, c.ID, Message)); } else { int cid = _channelId != -1 ? _channelId : Query.ListChannels(flags: true).Find(c => c.DefaultFlag).ID; Clients.Values.Where(c => c.ChannelID == cid && !IgnoredUsers.Contains(c.UID)).ForEach(c => Query.SendTextMessage(MessageTargetMode.Private, c.ID, Message)); } } protected override void OnStop(bool queryConnected) { if (!GreetAdmins || !queryConnected) return; foreach (Client admin in Bot.GetConnectedAdmins()) Query.SendTextMessage(MessageTargetMode.Private, admin.ID, "[B]axXezBot[/B] going offline. Good bye!"); } protected override void OnUnload() { //base.OnUnload(); //don't auto save config } public override void OnClientJoined(Client client, Channel source, Channel target, EventReason reason) { if (GreetAdmins && Bot.IsAdmin(client)) { Query.SendTextMessage(MessageTargetMode.Private, client.ID, _adminWelcomeMessage); return; } if (!GreetClients || IgnoredUsers.Contains(client.UID)) return; if (GreetClientsInEveryChannel || (_channelId == -1 && target.DefaultFlag) || _channelId == target.ID) Query.SendTextMessage(MessageTargetMode.Private, client.ID, Message); } public override void OnClientMoved(Client client, Channel target, EventReason reason, Client invoker) { if (!GreetClients || IgnoredUsers.Contains(client.UID)) return; if (_channelId == -1 && target.DefaultFlag || _channelId == target.ID) Query.SendTextMessage(MessageTargetMode.Private, client.ID, Message); } [Command("greeting status", Description = "Shows the greeting configuration", Role = CommandRole.Admin)] public CommandResponse Status(CommandContext ctx) { ctx.Reply("[B]Greetings[/B]"); ctx.Reply($" Greet admins: [B]{GreetAdmins}[/B]"); ctx.Reply($" Greet clients: [B]{GreetClients}[/B]"); if (Channel == null) ctx.Reply(" Channel: [COLOR=gray]default[/COLOR]"); else if (_channelId == -1) ctx.Reply($" Channel: [B]{Channel}[/B] [COLOR=red](not found)[/COLOR]"); else ctx.Reply($" Channel: [B]{Channel}[/B]"); ctx.Reply($" Greet on connect: [B]{GreetAlreadyConnectedClients}[/B]"); ctx.Reply($" Greet in every channel: [B]{GreetClientsInEveryChannel}[/B]"); ctx.Reply($" Ignored users: [B]{IgnoredUsers.Count}[/B]"); return ctx.Ok($" Message: {Message}"); } [Command("greeting toggle admin", Description = "Toggles greeting admins with bot status on connect", Role = CommandRole.Admin)] public CommandResponse ToggleAdmin(CommandContext ctx) { GreetAdmins = !GreetAdmins; Save(); return ctx.Ok($"Greet admins: [B]{GreetAdmins}[/B]."); } [Command("greeting toggle clients", Description = "Toggles greeting regular clients", Role = CommandRole.Admin)] public CommandResponse ToggleClients(CommandContext ctx) { GreetClients = !GreetClients; Save(); return ctx.Ok($"Greet clients: [B]{GreetClients}[/B]."); } [Command("greeting channel", Description = "Sets the channel to greet clients in (leave empty to use default)", Role = CommandRole.Admin)] public CommandResponse SetChannel(CommandContext ctx, [CommandParam("channel")] string channelName) { if (!Server.FindChannel(channelName, out var ch)) return ctx.Error($"Channel [B]{channelName}[/B] not found."); Channel = ch.Name; _channelId = ch.ID; Save(); return ctx.Ok($"Greeting channel set to [B]{ch.Name}[/B]."); } [Command("greeting message", Description = "Sets the greeting message (quote multi-word messages)", Role = CommandRole.Admin)] public CommandResponse SetMessage(CommandContext ctx, [CommandParam("message")] string message) { Message = message; Save(); return ctx.Ok("Greeting message updated."); } [Command("greeting toggle connect", Description = "Toggles greeting clients that are already connected on module start", Role = CommandRole.Admin)] public CommandResponse ToggleConnect(CommandContext ctx) { GreetAlreadyConnectedClients = !GreetAlreadyConnectedClients; Save(); return ctx.Ok($"Greet already connected clients: [B]{GreetAlreadyConnectedClients}[/B]."); } [Command("greeting toggle everywhere", Description = "Toggles greeting clients in every channel vs only the configured channel", Role = CommandRole.Admin)] public CommandResponse ToggleEverywhere(CommandContext ctx) { GreetClientsInEveryChannel = !GreetClientsInEveryChannel; Save(); return ctx.Ok($"Greet in every channel: [B]{GreetClientsInEveryChannel}[/B]."); } [Command("greeting stop", Description = "Stops greeting you")] public CommandResponse IgnoreUser(CommandContext ctx) { if (!IgnoredUsers.Contains(ctx.Invoker.UID)) { IgnoredUsers.Add(ctx.Invoker.UID); Save(); } return ctx.Ok("You will no longer be greeted."); } [Command("greeting start", Description = "Resumes greeting you")] public CommandResponse GreetUser(CommandContext ctx) { if (IgnoredUsers.Remove(ctx.Invoker.UID)) Save(); return ctx.Ok("You will be greeted again."); } } }