TrinityCore
Loading...
Searching...
No Matches
cs_guild.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18/* ScriptData
19Name: guild_commandscript
20%Complete: 100
21Comment: All guild related commands
22Category: commandscripts
23EndScriptData */
24
25#include "ScriptMgr.h"
26#include "AchievementMgr.h"
27#include "CharacterCache.h"
28#include "Chat.h"
29#include "ChatCommand.h"
30#include "Guild.h"
31#include "GuildMgr.h"
32#include "Language.h"
33#include "ObjectMgr.h"
34#include "Player.h"
35#include "RBAC.h"
36#include "Util.h"
37#include <iomanip>
38
39#if TRINITY_COMPILER == TRINITY_COMPILER_GNU
40#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
41#endif
42
43using namespace Trinity::ChatCommands;
45{
46public:
47 guild_commandscript() : CommandScript("guild_commandscript") { }
48
49 std::span<ChatCommandBuilder const> GetCommands() const override
50 {
51 static ChatCommandTable guildCommandTable =
52 {
61 };
62 static ChatCommandTable commandTable =
63 {
64 { "guild", rbac::RBAC_PERM_COMMAND_GUILD, true, nullptr, "", guildCommandTable },
65 };
66 return commandTable;
67 }
68
77 static bool HandleGuildCreateCommand(ChatHandler* handler, char const* args)
78 {
79 if (!*args)
80 return false;
81
82 // if not guild name only (in "") then player name
83 Player* target;
84 if (!handler->extractPlayerTarget(*args != '"' ? (char*)args : nullptr, &target))
85 return false;
86
87 char* tailStr = *args != '"' ? strtok(nullptr, "") : (char*)args;
88 if (!tailStr)
89 return false;
90
91 char* guildStr = handler->extractQuotedArg(tailStr);
92 if (!guildStr)
93 return false;
94
95 std::string guildName = guildStr;
96
97 if (target->GetGuildId())
98 {
100 handler->SetSentErrorMessage(true);
101 return false;
102 }
103
104 if (sGuildMgr->GetGuildByName(guildName))
105 {
107 handler->SetSentErrorMessage(true);
108 return false;
109 }
110
111 if (sObjectMgr->IsReservedName(guildName) || !sObjectMgr->IsValidCharterName(guildName))
112 {
114 handler->SetSentErrorMessage(true);
115 return false;
116 }
117
118 Guild* guild = new Guild;
119 if (!guild->Create(target, guildName))
120 {
121 delete guild;
123 handler->SetSentErrorMessage(true);
124 return false;
125 }
126
127 sGuildMgr->AddGuild(guild);
128
129 return true;
130 }
131
132 static bool HandleGuildDeleteCommand(ChatHandler* handler, char const* args)
133 {
134 if (!*args)
135 return false;
136
137 char* guildStr = handler->extractQuotedArg((char*)args);
138 if (!guildStr)
139 return false;
140
141 std::string guildName = guildStr;
142
143 Guild* targetGuild = sGuildMgr->GetGuildByName(guildName);
144 if (!targetGuild)
145 return false;
146
147 targetGuild->Disband();
148 return true;
149 }
150
151 static bool HandleGuildInviteCommand(ChatHandler* handler, char const* args)
152 {
153 if (!*args)
154 return false;
155
156 // if not guild name only (in "") then player name
157 ObjectGuid targetGuid;
158 if (!handler->extractPlayerTarget(*args != '"' ? (char*)args : nullptr, nullptr, &targetGuid))
159 return false;
160
161 char* tailStr = *args != '"' ? strtok(nullptr, "") : (char*)args;
162 if (!tailStr)
163 return false;
164
165 char* guildStr = handler->extractQuotedArg(tailStr);
166 if (!guildStr)
167 return false;
168
169 std::string guildName = guildStr;
170 Guild* targetGuild = sGuildMgr->GetGuildByName(guildName);
171 if (!targetGuild)
172 return false;
173
174 // player's guild membership checked in AddMember before add
175 CharacterDatabaseTransaction trans(nullptr);
176 return targetGuild->AddMember(trans, targetGuid);
177 }
178
179 static bool HandleGuildUninviteCommand(ChatHandler* handler, char const* args)
180 {
181 Player* target;
182 ObjectGuid targetGuid;
183 if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid))
184 return false;
185
186 ObjectGuid::LowType guildId = target ? target->GetGuildId() : sCharacterCache->GetCharacterGuildIdByGuid(targetGuid);
187 if (!guildId)
188 return false;
189
190 Guild* targetGuild = sGuildMgr->GetGuildById(guildId);
191 if (!targetGuild)
192 return false;
193
194 CharacterDatabaseTransaction trans(nullptr);
195 targetGuild->DeleteMember(trans, targetGuid, false, true);
196 return true;
197 }
198
200 {
201 if (!player)
202 player = PlayerIdentifier::FromTargetOrSelf(handler);
203 if (!player)
204 return false;
205
206 ObjectGuid::LowType guildId = player->IsConnected() ? player->GetConnectedPlayer()->GetGuildId() : sCharacterCache->GetCharacterGuildIdByGuid(*player);
207 if (!guildId)
208 return false;
209
210 Guild* targetGuild = sGuildMgr->GetGuildById(guildId);
211 if (!targetGuild)
212 return false;
213
214 return targetGuild->ChangeMemberRank(nullptr, *player, GuildRankId(rank));
215 }
216
217 static bool HandleGuildRenameCommand(ChatHandler* handler, char const* _args)
218 {
219 if (!*_args)
220 return false;
221
222 char *args = (char *)_args;
223
224 char const* oldGuildStr = handler->extractQuotedArg(args);
225 if (!oldGuildStr)
226 {
228 handler->SetSentErrorMessage(true);
229 return false;
230 }
231
232 char const* newGuildStr = handler->extractQuotedArg(strtok(nullptr, ""));
233 if (!newGuildStr)
234 {
236 handler->SetSentErrorMessage(true);
237 return false;
238 }
239
240 Guild* guild = sGuildMgr->GetGuildByName(oldGuildStr);
241 if (!guild)
242 {
243 handler->PSendSysMessage(LANG_COMMAND_COULDNOTFIND, oldGuildStr);
244 handler->SetSentErrorMessage(true);
245 return false;
246 }
247
248 if (sGuildMgr->GetGuildByName(newGuildStr))
249 {
251 handler->SetSentErrorMessage(true);
252 return false;
253 }
254
255 if (!guild->SetName(newGuildStr))
256 {
258 handler->SetSentErrorMessage(true);
259 return false;
260 }
261
262 handler->PSendSysMessage(LANG_GUILD_RENAME_DONE, oldGuildStr, newGuildStr);
263 return true;
264 }
265
267 {
268 Guild* guild = nullptr;
269
270 if (guildIdentifier)
271 {
272 if (ObjectGuid::LowType const* guid = std::get_if<ObjectGuid::LowType>(&*guildIdentifier))
273 guild = sGuildMgr->GetGuildById(*guid);
274 else
275 guild = sGuildMgr->GetGuildByName(guildIdentifier->get<std::string_view>());
276 }
277 else if (Optional<PlayerIdentifier> target = PlayerIdentifier::FromTargetOrSelf(handler); target && target->IsConnected())
278 guild = target->GetConnectedPlayer()->GetGuild();
279
280 if (!guild)
281 return false;
282
283 // Display Guild Information
284 handler->PSendSysMessage(LANG_GUILD_INFO_NAME, guild->GetName().c_str(), std::to_string(guild->GetId()).c_str()); // Guild Id + Name
285
286 std::string guildMasterName;
287 if (sCharacterCache->GetCharacterNameByGuid(guild->GetLeaderGUID(), guildMasterName))
288 handler->PSendSysMessage(LANG_GUILD_INFO_GUILD_MASTER, guildMasterName.c_str(), guild->GetLeaderGUID().ToString().c_str()); // Guild Master
289
290 // Format creation date
291 char createdDateStr[20];
292 time_t createdDate = guild->GetCreatedDate();
293 tm localTm;
294 strftime(createdDateStr, 20, "%Y-%m-%d %H:%M:%S", localtime_r(&createdDate, &localTm));
295
296 handler->PSendSysMessage(LANG_GUILD_INFO_CREATION_DATE, createdDateStr); // Creation Date
297 handler->PSendSysMessage(LANG_GUILD_INFO_MEMBER_COUNT, guild->GetMembersCount()); // Number of Members
298 handler->PSendSysMessage(LANG_GUILD_INFO_BANK_GOLD, std::to_string(guild->GetBankMoney() / 100 / 100).c_str()); // Bank Gold (in gold coins)
299 handler->PSendSysMessage(LANG_GUILD_INFO_LEVEL, guild->GetLevel()); // Level
300 handler->PSendSysMessage(LANG_GUILD_INFO_MOTD, guild->GetMOTD().c_str()); // Message of the Day
301 handler->PSendSysMessage(LANG_GUILD_INFO_EXTRA_INFO, guild->GetInfo().c_str()); // Extra Information
302 return true;
303 }
304
306 {
307 std::string_view titleAndSummaryColor = handler->IsConsole() ? ""sv : "|cff00ff00"sv;
308 std::string_view tableHeaderColor = handler->IsConsole() ? ""sv : "|cff00ffff"sv;
309 std::string_view resetColor = handler->IsConsole() ? ""sv : "|r"sv;
310
311 handler->PSendSysMessage(LANG_GUILD_LIST_TITLE, titleAndSummaryColor, resetColor);
312 handler->PSendSysMessage(LANG_GUILD_LIST_HEADER, tableHeaderColor, resetColor);
313
314 GuildMgr::GuildContainer const& guildStore = sGuildMgr->GetGuildStore();
315
316 for (auto const& [id, g] : guildStore)
317 {
318 std::string gmName;
319 if (!sCharacterCache->GetCharacterNameByGuid(g->GetLeaderGUID(), gmName))
320 gmName = "---";
321
323 id,
324 g->GetName().c_str(),
325 gmName.c_str(),
326 TimeToTimestampStr(g->GetCreatedDate()).c_str(),
327 g->GetMembersCount(),
328 g->GetBankMoney() / GOLD
329 );
330 }
331
332 handler->PSendSysMessage(LANG_GUILD_LIST_TOTAL, titleAndSummaryColor, guildStore.size(), resetColor);
333 return true;
334 }
335};
336
#define sCharacterCache
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
uint8_t uint8
Definition Define.h:156
#define sGuildMgr
Definition GuildMgr.h:76
GuildRankId
Definition Guild.h:96
@ LANG_GUILD_NOT_CREATED
Definition Language.h:572
@ LANG_GUILD_INFO_GUILD_MASTER
Definition Language.h:954
@ LANG_GUILD_INFO_BANK_GOLD
Definition Language.h:957
@ LANG_PLAYER_IN_GUILD
Definition Language.h:571
@ LANG_GUILD_RENAME_DONE
Definition Language.h:129
@ LANG_GUILD_LIST_ROW
Definition Language.h:1002
@ LANG_COMMAND_COULDNOTFIND
Definition Language.h:497
@ LANG_GUILD_LIST_TITLE
Definition Language.h:1000
@ LANG_GUILD_LIST_HEADER
Definition Language.h:1001
@ LANG_GUILD_INFO_EXTRA_INFO
Definition Language.h:959
@ LANG_GUILD_INFO_MEMBER_COUNT
Definition Language.h:956
@ LANG_GUILD_RENAME_ALREADY_EXISTS
Definition Language.h:128
@ LANG_GUILD_INFO_LEVEL
Definition Language.h:991
@ LANG_BAD_VALUE
Definition Language.h:149
@ LANG_GUILD_INFO_MOTD
Definition Language.h:958
@ LANG_GUILD_INFO_NAME
Definition Language.h:953
@ LANG_GUILD_LIST_TOTAL
Definition Language.h:1003
@ LANG_INSERT_GUILD_NAME
Definition Language.h:569
@ LANG_GUILD_INFO_CREATION_DATE
Definition Language.h:955
#define sObjectMgr
Definition ObjectMgr.h:1885
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
Role Based Access Control related classes definition.
@ GOLD
std::string TimeToTimestampStr(time_t t)
Definition Util.cpp:254
char * extractQuotedArg(char *args)
Definition Chat.cpp:547
void SetSentErrorMessage(bool val)
Definition Chat.h:127
void PSendSysMessage(char const *fmt, Args &&... args)
Definition Chat.h:62
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition Chat.cpp:111
bool extractPlayerTarget(char *args, Player **player, ObjectGuid *player_guid=nullptr, std::string *player_name=nullptr)
Definition Chat.cpp:490
bool IsConsole() const
Definition Chat.h:41
std::unordered_map< ObjectGuid::LowType, Trinity::unique_trackable_ptr< Guild > > GuildContainer
Definition GuildMgr.h:37
Definition Guild.h:329
uint64 GetBankMoney() const
Definition Guild.h:761
ObjectGuid GetLeaderGUID() const
Definition Guild.h:756
ObjectGuid::LowType GetId() const
Definition Guild.h:754
uint8 GetLevel() const
Definition Guild.h:868
bool AddMember(CharacterDatabaseTransaction trans, ObjectGuid guid, Optional< GuildRankId > rankId={})
Definition Guild.cpp:2816
std::string const & GetName() const
Definition Guild.h:757
bool SetName(std::string_view name)
Definition Guild.cpp:1312
bool ChangeMemberRank(CharacterDatabaseTransaction trans, ObjectGuid guid, GuildRankId newRank)
Definition Guild.cpp:3021
bool Create(Player *pLeader, std::string_view name)
Definition Guild.cpp:1138
time_t GetCreatedDate() const
Definition Guild.h:760
bool DeleteMember(CharacterDatabaseTransaction trans, ObjectGuid guid, bool isDisbanding=false, bool isKicked=false)
Definition Guild.cpp:2958
std::string const & GetInfo() const
Definition Guild.h:759
void Disband()
Definition Guild.cpp:1199
std::string const & GetMOTD() const
Definition Guild.h:758
uint32 GetMembersCount() const
Definition Guild.h:852
std::string ToString() const
uint64 LowType
Definition ObjectGuid.h:321
ObjectGuid::LowType GetGuildId() const
Definition Player.h:2164
static bool HandleGuildCreateCommand(ChatHandler *handler, char const *args)
GM command level 3 - Create a guild.
Definition cs_guild.cpp:77
static bool HandleGuildDeleteCommand(ChatHandler *handler, char const *args)
Definition cs_guild.cpp:132
static bool HandleGuildListCommand(ChatHandler *handler)
Definition cs_guild.cpp:305
static bool HandleGuildInfoCommand(ChatHandler *handler, Optional< Variant< ObjectGuid::LowType, std::string_view > > const &guildIdentifier)
Definition cs_guild.cpp:266
static bool HandleGuildUninviteCommand(ChatHandler *handler, char const *args)
Definition cs_guild.cpp:179
static bool HandleGuildRenameCommand(ChatHandler *handler, char const *_args)
Definition cs_guild.cpp:217
std::span< ChatCommandBuilder const > GetCommands() const override
Definition cs_guild.cpp:49
static bool HandleGuildRankCommand(ChatHandler *handler, Optional< PlayerIdentifier > player, uint8 rank)
Definition cs_guild.cpp:199
static bool HandleGuildInviteCommand(ChatHandler *handler, char const *args)
Definition cs_guild.cpp:151
void AddSC_guild_commandscript()
Definition cs_guild.cpp:337
ChatCommandBuilder const [] ChatCommandTable
Definition ChatCommand.h:49
@ RBAC_PERM_COMMAND_GUILD_UNINVITE
Definition RBAC.h:277
@ RBAC_PERM_COMMAND_GUILD_INVITE
Definition RBAC.h:276
@ RBAC_PERM_COMMAND_GUILD
Definition RBAC.h:273
@ RBAC_PERM_COMMAND_GUILD_RANK
Definition RBAC.h:278
@ RBAC_PERM_COMMAND_GUILD_INFO
Definition RBAC.h:666
@ RBAC_PERM_COMMAND_GUILD_RENAME
Definition RBAC.h:279
@ RBAC_PERM_COMMAND_GUILD_CREATE
Definition RBAC.h:274
@ RBAC_PERM_COMMAND_GUILD_DELETE
Definition RBAC.h:275
static Optional< PlayerIdentifier > FromTargetOrSelf(ChatHandler *handler)