Skip to content

Commit 59bb77a

Browse files
committed
Merge branch 'dev'
2 parents 7940e80 + 1b9820f commit 59bb77a

13 files changed

Lines changed: 378 additions & 85 deletions

Source/Commands/AdminCommands.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,18 @@
1717
using PRTelegramBot.Helpers;
1818
using Cs2Telegram.Models;
1919
using CounterStrikeSharp.API.Modules.Memory;
20+
using Cs2Telegram.TelegramEvents;
2021

2122
namespace Cs2Telegram.Commands
2223
{
2324
public static class AdminCommands
2425
{
25-
public static async Task AccessDenied(ITelegramBotClient botClient, Update update)
26-
{
27-
string msg = "Access denied";
28-
Helper.SendMessage(botClient, update.GetChatId(), msg);
29-
}
30-
3126
[ReplyMenuHandler(Constants.ADMIN_MENU_BUTTON)]
3227
public static async Task AdminMenu(ITelegramBotClient botClient, Update update)
3328
{
3429
if (!botClient.IsAdmin(update.GetChatId()))
3530
{
36-
await AccessDenied(botClient, update);
31+
await CommonEvents.AccessDenied(botClient, update);
3732
return;
3833
}
3934

Source/Commands/CommonCommands.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,22 @@ public static async Task Status(ITelegramBotClient botClient, Update update)
4848
var allCount = botCount + userCount;
4949
var maxPlayers = Server.MaxPlayers;
5050
string mapName = Server.MapName;
51+
5152
var serverTime = Server.EngineTime;
53+
5254
string hostname = "";
5355

5456
var hostnameCvar = ConVar.Find("hostname");
55-
57+
var gameTypeCvar = ConVar.Find("game_type");
58+
var gameModeCvar = ConVar.Find("game_mode");
59+
string gametype = Helper.GetGameModeWithGameType(gameTypeCvar.GetPrimitiveValue<int>(), gameModeCvar.GetPrimitiveValue<int>());
5660
if (hostnameCvar != null)
5761
{
5862
hostname = hostnameCvar.StringValue;
5963
}
6064

6165
var msg = $"🌐 Server '{hostname}':\n" +
66+
$"🕹️ Game mode: {gametype}\n" +
6267
$"👨‍👩‍👧‍👦 Players: {allCount}/{maxPlayers}\n" +
6368
$"👦 Humans: {userCount}\n" +
6469
$"🤖 Bots: {botCount}\n" +

Source/Commands/PlayerCommands.cs

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
using CounterStrikeSharp.API;
2+
using Cs2Telegram.Enums;
3+
using Cs2Telegram.Models;
4+
using Cs2Telegram.TelegramEvents;
5+
using PRTelegramBot.Attributes;
6+
using PRTelegramBot.Helpers.TG;
7+
using PRTelegramBot.Models.InlineButtons;
8+
using PRTelegramBot.Models.Interface;
9+
using PRTelegramBot.Models;
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
using System.Text;
14+
using System.Threading.Tasks;
15+
using Telegram.Bot.Types;
16+
using Telegram.Bot;
17+
using PRTelegramBot.Extensions;
18+
using PRTelegramBot.Models.CallbackCommands;
19+
using CounterStrikeSharp.API.Modules.Utils;
20+
21+
namespace Cs2Telegram.Commands
22+
{
23+
public static class PlayerCommands
24+
{
25+
const string PLAYER_NOT_VALID = "Player not found or player is no valid";
26+
27+
[ReplyMenuHandler(Constants.SERVER_PLAYERS_INFO_BUTTON)]
28+
[InlineCallbackHandler<HeaderCommand>(HeaderCommand.PlayerInfoList)]
29+
public static async Task PlayersInfo(ITelegramBotClient botClient, Update update)
30+
{
31+
if (!botClient.IsAdmin(update.GetChatId()))
32+
{
33+
await CommonEvents.AccessDenied(botClient, update);
34+
return;
35+
}
36+
37+
var inlineMenuItems = new List<IInlineContent>();
38+
Server.NextFrame(() =>
39+
{
40+
var players = Utilities.GetPlayers();
41+
foreach (var player in players)
42+
{
43+
var commandItem = new InlineCallback<EntityTCommand<int>>($"{player.PlayerName}{(player.IsBot ? " [Bot]" : "")}", HeaderCommand.PlayerInfo, new EntityTCommand<int>(player.UserId != null ? player.UserId.Value : -1));
44+
inlineMenuItems.Add(commandItem);
45+
}
46+
47+
var options = new OptionMessage();
48+
string msg = "Players not found";
49+
if (inlineMenuItems.Count > 0)
50+
{
51+
msg = "Players on server:";
52+
options.MenuInlineKeyboardMarkup = MenuGenerator.InlineKeyboard(MenuGenerator.InlineButtons(1, inlineMenuItems));
53+
}
54+
else
55+
{
56+
options.MenuReplyKeyboardMarkup = botClient.GenerateOnlyMenu(update.GetChatId());
57+
}
58+
var command = InlineCallback<EntityTCommand<int>>.GetCommandByCallbackOrNull(update.CallbackQuery?.Data ?? "");
59+
if(command != null)
60+
{
61+
Helper.EditMessage(botClient, update, msg, options);
62+
}
63+
else
64+
{
65+
Helper.SendMessage(botClient, update, msg, options);
66+
}
67+
68+
});
69+
}
70+
71+
[InlineCallbackHandler<HeaderCommand>(HeaderCommand.PlayerInfo)]
72+
public static async Task PlayerInfoHandler(ITelegramBotClient botClient, Update update)
73+
{
74+
if (!botClient.IsAdmin(update.GetChatId()))
75+
{
76+
await CommonEvents.AccessDenied(botClient, update);
77+
return;
78+
}
79+
80+
var command = InlineCallback<EntityTCommand<int>>.GetCommandByCallbackOrNull(update.CallbackQuery.Data);
81+
if (command != null && command.Data.EntityId != -1)
82+
{
83+
Server.NextFrame(() =>
84+
{
85+
int userId = command.Data.EntityId;
86+
string msg = PLAYER_NOT_VALID;
87+
var options = new OptionMessage();
88+
var player = Utilities.GetPlayerFromUserid(userId);
89+
if (player != null && player.IsValid)
90+
{
91+
msg =
92+
$"Player name:{player.PlayerName}\n" +
93+
$"Clan name: {player.ClanName}\n" +
94+
$"Score: {player.Score}\n" +
95+
$"IP: {player.IpAddress ?? ""}\n" +
96+
//$"Time: {TimeSpan.FromSeconds(player.).ToReadableString()}\n" +
97+
$"Ping: {player.Ping}\n" +
98+
$"SteamId: {player.AuthorizedSteamID?.ToString()}\n";
99+
100+
//var changeTeamTButton = new InlineCallback<ChangeTeamPlayerCommand>($"Team T", HeaderCommand.PlayerChangeTeam, new ChangeTeamPlayerCommand(player.UserId != null ? player.UserId.Value : -1, CsTeam.Terrorist));
101+
//var changeTeamCTButton = new InlineCallback<ChangeTeamPlayerCommand>($"Team CT", HeaderCommand.PlayerChangeTeam, new ChangeTeamPlayerCommand(player.UserId != null ? player.UserId.Value : -1, CsTeam.CounterTerrorist));
102+
//var changeTeamSpectatorButton = new InlineCallback<ChangeTeamPlayerCommand>($"Team Spectator", HeaderCommand.PlayerChangeTeam, new ChangeTeamPlayerCommand(player.UserId != null ? player.UserId.Value : -1, CsTeam.Spectator));
103+
var kickCommandItem = new InlineCallback<EntityTCommand<int>>($"Kick", HeaderCommand.PlayerKick, new EntityTCommand<int>(player.UserId != null ? player.UserId.Value : -1));
104+
var BackCommandItem = new InlineCallback<EntityTCommand<int>>($"Back", HeaderCommand.PlayerInfoList, new EntityTCommand<int>(-1));
105+
//var killCommandItem = new InlineCallback<EntityTCommand<int>>($"Kill", HeaderCommand.PlayerKill, new EntityTCommand<int>(player.UserId != null ? player.UserId.Value : -1));
106+
var menuTeamButtons = MenuGenerator.InlineButtons(2,new List<IInlineContent> { kickCommandItem, BackCommandItem });
107+
var menu = MenuGenerator.InlineKeyboard(menuTeamButtons);
108+
options.MenuInlineKeyboardMarkup = menu;
109+
110+
}
111+
112+
Helper.EditMessage(botClient, update, msg, options);
113+
});
114+
}
115+
}
116+
117+
[InlineCallbackHandler<HeaderCommand>(HeaderCommand.PlayerKick)]
118+
public static async Task PlayerKickHandler(ITelegramBotClient botClient, Update update)
119+
{
120+
if (!botClient.IsAdmin(update.GetChatId()))
121+
{
122+
await CommonEvents.AccessDenied(botClient, update);
123+
return;
124+
}
125+
126+
var command = InlineCallback<EntityTCommand<int>>.GetCommandByCallbackOrNull(update.CallbackQuery.Data);
127+
if (command != null && command.Data.EntityId != -1)
128+
{
129+
Server.NextFrame(() =>
130+
{
131+
int userId = command.Data.EntityId;
132+
string msg = PLAYER_NOT_VALID;
133+
var options = new OptionMessage();
134+
var player = Utilities.GetPlayerFromUserid(userId);
135+
if (player != null && player.IsValid)
136+
{
137+
msg = $"Server kicked {player.PlayerName}";
138+
Server.ExecuteCommand($"kickid {player.UserId}");
139+
}
140+
141+
Helper.EditMessage(botClient, update, msg, options);
142+
});
143+
}
144+
}
145+
146+
//[InlineCallbackHandler<HeaderCommand>(HeaderCommand.PlayerKill)]
147+
//public static async Task PlayerKillHandler(ITelegramBotClient botClient, Update update)
148+
//{
149+
// if (!botClient.IsAdmin(update.GetChatId()))
150+
// {
151+
// await CommonEvents.AccessDenied(botClient, update);
152+
// return;
153+
// }
154+
155+
// var command = InlineCallback<EntityTCommand<int>>.GetCommandByCallbackOrNull(update.CallbackQuery.Data);
156+
// if (command != null && command.Data.EntityId != -1)
157+
// {
158+
// Server.NextFrame(() =>
159+
// {
160+
// int userId = command.Data.EntityId;
161+
// string msg = PLAYER_NOT_VALID;
162+
// var options = new OptionMessage();
163+
// var player = Utilities.GetPlayerFromUserid(userId);
164+
// if (player != null && player.IsValid)
165+
// {
166+
// if(player.PawnIsAlive)
167+
// {
168+
// msg = $"Player {player.PlayerName} kill";
169+
// player.CommitSuicide(false, true);
170+
// }
171+
// else
172+
// {
173+
// msg = $"Player {player.PlayerName} already dead";
174+
// }
175+
// }
176+
177+
// Helper.EditMessage(botClient, update, msg, options);
178+
// });
179+
// }
180+
//}
181+
182+
183+
//[InlineCallbackHandler<HeaderCommand>(HeaderCommand.PlayerChangeTeam)]
184+
//public static async Task PlayerChangeTeamHandler(ITelegramBotClient botClient, Update update)
185+
//{
186+
// if (!botClient.IsAdmin(update.GetChatId()))
187+
// {
188+
// await CommonEvents.AccessDenied(botClient, update);
189+
// return;
190+
// }
191+
192+
// var command = InlineCallback<ChangeTeamPlayerCommand>.GetCommandByCallbackOrNull(update.CallbackQuery.Data);
193+
// if (command != null && command.Data.UserId != -1)
194+
// {
195+
// Server.NextFrame(() =>
196+
// {
197+
// int userId = command.Data.UserId;
198+
// string msg = PLAYER_NOT_VALID;
199+
// var options = new OptionMessage();
200+
// var player = Utilities.GetPlayerFromUserid(userId);
201+
// if (player != null && player.IsValid)
202+
// {
203+
// msg = "ChangeTeam";
204+
// player.ChangeTeam(CsTeam.CounterTerrorist);
205+
// }
206+
207+
// Helper.SendMessage(botClient, update, msg, options);
208+
// });
209+
// }
210+
//}
211+
}
212+
}

Source/Commands/ServerCommands.cs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
using PRTelegramBot.Models.CallbackCommands;
1717
using PRTelegramBot.Models.Interface;
1818
using PRTelegramBot.Helpers.TG;
19+
using Cs2Telegram.Enums;
20+
using Cs2Telegram.TelegramEvents;
1921

2022
namespace Cs2Telegram.Commands
2123
{
@@ -30,28 +32,13 @@ public static async Task ExecuteCommandInServer(ITelegramBotClient botClient, Up
3032
{
3133
if (!botClient.IsAdmin(update.GetChatId()))
3234
{
33-
await AdminCommands.AccessDenied(botClient, update);
35+
await CommonEvents.AccessDenied(botClient, update);
3436
return;
3537
}
3638

37-
var pathCommands = Path.Combine(TelegramCore.PluginPath, TelegramCore.FILE_SERVER_COMMANDS);
3839

39-
if(!File.Exists(pathCommands))
40-
{
41-
File.Create(pathCommands);
42-
}
43-
44-
var lines = File.ReadAllLines(pathCommands);
45-
var inlineMenuItems = new List<IInlineContent>();
46-
if (lines.Length > 0)
47-
{
48-
foreach (var command in lines.Where(command => !string.IsNullOrWhiteSpace(command)))
49-
{
50-
var commandItem = new InlineCallback<ServerExecuteTCommand>(command, HeadersCommads.ExecuteServerCommand, new ServerExecuteTCommand(command));
51-
inlineMenuItems.Add(commandItem);
52-
}
53-
}
5440

41+
var inlineMenuItems = Helper.GetServerCommandsItems();
5542
var options = new OptionMessage();
5643
if(inlineMenuItems.Count > 0)
5744
{
@@ -87,12 +74,12 @@ public static async Task ExecuteCommandInServerHandler(ITelegramBotClient botCli
8774
}
8875
}
8976

90-
[InlineCallbackHandler<HeadersCommads>(HeadersCommads.ExecuteServerCommand)]
77+
[InlineCallbackHandler<HeaderCommand>(HeaderCommand.ExecuteServerCommand)]
9178
public static async Task ExecuteCommandInServerHandler(ITelegramBotClient botClient, Update update)
9279
{
9380
if (!botClient.IsAdmin(update.GetChatId()))
9481
{
95-
await AdminCommands.AccessDenied(botClient, update);
82+
await CommonEvents.AccessDenied(botClient, update);
9683
return;
9784
}
9885

@@ -106,11 +93,17 @@ public static async Task ExecuteCommandInServerHandler(ITelegramBotClient botCli
10693
{
10794
Server.ExecuteCommand(serverCommand);
10895
});
109-
Helper.SendMessage(botClient, update, $"🗯️ Server try execute command: {serverCommand}\n\n{SERVER_COMMAND_MSG}");
96+
var inlineMenuItems = Helper.GetServerCommandsItems();
97+
var options = new OptionMessage();
98+
if (inlineMenuItems.Count > 0)
99+
{
100+
options.MenuInlineKeyboardMarkup = MenuGenerator.InlineKeyboard(MenuGenerator.InlineButtons(1, inlineMenuItems));
101+
}
102+
Helper.EditMessage(botClient, update, $"🗯️ Server try execute command: {serverCommand}\n\n{SERVER_COMMAND_MSG}\nGuid:{Guid.NewGuid()}", options);
110103
}
111104
else
112105
{
113-
Helper.SendMessage(botClient, update, $"Error empty message, try again");
106+
Helper.EditMessage(botClient, update, $"Error empty message, try again");
114107
}
115108
}
116109
}
@@ -120,7 +113,7 @@ public static async Task SendMessageToServer(ITelegramBotClient botClient, Updat
120113
{
121114
if (!botClient.IsAdmin(update.GetChatId()))
122115
{
123-
await AdminCommands.AccessDenied(botClient, update);
116+
await CommonEvents.AccessDenied(botClient, update);
124117
return;
125118
}
126119

Source/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static class Constants
1919
#region AdminMenu
2020
public const string SERVER_COMMAND_BUTTON = "Server command";
2121
public const string SERVER_SEND_MESSAGE_BUTTON = "Server message";
22+
public const string SERVER_PLAYERS_INFO_BUTTON = "Players info";
2223
#endregion
2324
}
2425
}

Source/Cs2Telegram.csproj

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Nullable>enable</Nullable>
77
<InvariantGlobalization>true</InvariantGlobalization>
88
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
9-
<Version>0.2.1</Version>
9+
<Version>0.2.2</Version>
1010
</PropertyGroup>
1111

1212
<ItemGroup>
@@ -23,10 +23,4 @@
2323
</Reference>
2424
</ItemGroup>
2525

26-
<ItemGroup>
27-
<None Update="favorite_server_command.txt">
28-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
29-
</None>
30-
</ItemGroup>
31-
3226
</Project>

0 commit comments

Comments
 (0)