TrinityCore
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 <iomanip>
37
38#if TRINITY_COMPILER == TRINITY_COMPILER_GNU
39#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
40#endif
41
42using namespace Trinity::ChatCommands;
44{
45public:
46 guild_commandscript() : CommandScript("guild_commandscript") { }
47
48 std::vector<ChatCommand> GetCommands() const override
49 {
50 static std::vector<ChatCommand> guildCommandTable =
51 {
59 };
60 static std::vector<ChatCommand> commandTable =
61 {
62 { "guild", rbac::RBAC_PERM_COMMAND_GUILD, true, nullptr, "", guildCommandTable },
63 };
64 return commandTable;
65 }
66
75 static bool HandleGuildCreateCommand(ChatHandler* handler, char const* args)
76 {
77 if (!*args)
78 return false;
79
80 // if not guild name only (in "") then player name
81 Player* target;
82 if (!handler->extractPlayerTarget(*args != '"' ? (char*)args : nullptr, &target))
83 return false;
84
85 char* tailStr = *args != '"' ? strtok(nullptr, "") : (char*)args;
86 if (!tailStr)
87 return false;
88
89 char* guildStr = handler->extractQuotedArg(tailStr);
90 if (!guildStr)
91 return false;
92
93 std::string guildName = guildStr;
94
95 if (target->GetGuildId())
96 {
98 handler->SetSentErrorMessage(true);
99 return false;
100 }
101
102 if (sGuildMgr->GetGuildByName(guildName))
103 {
105 handler->SetSentErrorMessage(true);
106 return false;
107 }
108
109 if (sObjectMgr->IsReservedName(guildName) || !sObjectMgr->IsValidCharterName(guildName))
110 {
112 handler->SetSentErrorMessage(true);
113 return false;
114 }
115
116 Guild* guild = new Guild;
117 if (!guild->Create(target, guildName))
118 {
119 delete guild;
121 handler->SetSentErrorMessage(true);
122 return false;
123 }
124
125 sGuildMgr->AddGuild(guild);
126
127 return true;
128 }
129
130 static bool HandleGuildDeleteCommand(ChatHandler* handler, char const* args)
131 {
132 if (!*args)
133 return false;
134
135 char* guildStr = handler->extractQuotedArg((char*)args);
136 if (!guildStr)
137 return false;
138
139 std::string guildName = guildStr;
140
141 Guild* targetGuild = sGuildMgr->GetGuildByName(guildName);
142 if (!targetGuild)
143 return false;
144
145 targetGuild->Disband();
146 return true;
147 }
148
149 static bool HandleGuildInviteCommand(ChatHandler* handler, char const* args)
150 {
151 if (!*args)
152 return false;
153
154 // if not guild name only (in "") then player name
155 ObjectGuid targetGuid;
156 if (!handler->extractPlayerTarget(*args != '"' ? (char*)args : nullptr, nullptr, &targetGuid))
157 return false;
158
159 char* tailStr = *args != '"' ? strtok(nullptr, "") : (char*)args;
160 if (!tailStr)
161 return false;
162
163 char* guildStr = handler->extractQuotedArg(tailStr);
164 if (!guildStr)
165 return false;
166
167 std::string guildName = guildStr;
168 Guild* targetGuild = sGuildMgr->GetGuildByName(guildName);
169 if (!targetGuild)
170 return false;
171
172 // player's guild membership checked in AddMember before add
173 CharacterDatabaseTransaction trans(nullptr);
174 return targetGuild->AddMember(trans, targetGuid);
175 }
176
177 static bool HandleGuildUninviteCommand(ChatHandler* handler, char const* args)
178 {
179 Player* target;
180 ObjectGuid targetGuid;
181 if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid))
182 return false;
183
184 ObjectGuid::LowType guildId = target ? target->GetGuildId() : sCharacterCache->GetCharacterGuildIdByGuid(targetGuid);
185 if (!guildId)
186 return false;
187
188 Guild* targetGuild = sGuildMgr->GetGuildById(guildId);
189 if (!targetGuild)
190 return false;
191
192 CharacterDatabaseTransaction trans(nullptr);
193 targetGuild->DeleteMember(trans, targetGuid, false, true);
194 return true;
195 }
196
198 {
199 if (!player)
200 player = PlayerIdentifier::FromTargetOrSelf(handler);
201 if (!player)
202 return false;
203
204 ObjectGuid::LowType guildId = player->IsConnected() ? player->GetConnectedPlayer()->GetGuildId() : sCharacterCache->GetCharacterGuildIdByGuid(*player);
205 if (!guildId)
206 return false;
207
208 Guild* targetGuild = sGuildMgr->GetGuildById(guildId);
209 if (!targetGuild)
210 return false;
211
212 return targetGuild->ChangeMemberRank(nullptr, *player, GuildRankId(rank));
213 }
214
215 static bool HandleGuildRenameCommand(ChatHandler* handler, char const* _args)
216 {
217 if (!*_args)
218 return false;
219
220 char *args = (char *)_args;
221
222 char const* oldGuildStr = handler->extractQuotedArg(args);
223 if (!oldGuildStr)
224 {
226 handler->SetSentErrorMessage(true);
227 return false;
228 }
229
230 char const* newGuildStr = handler->extractQuotedArg(strtok(nullptr, ""));
231 if (!newGuildStr)
232 {
234 handler->SetSentErrorMessage(true);
235 return false;
236 }
237
238 Guild* guild = sGuildMgr->GetGuildByName(oldGuildStr);
239 if (!guild)
240 {
241 handler->PSendSysMessage(LANG_COMMAND_COULDNOTFIND, oldGuildStr);
242 handler->SetSentErrorMessage(true);
243 return false;
244 }
245
246 if (sGuildMgr->GetGuildByName(newGuildStr))
247 {
249 handler->SetSentErrorMessage(true);
250 return false;
251 }
252
253 if (!guild->SetName(newGuildStr))
254 {
256 handler->SetSentErrorMessage(true);
257 return false;
258 }
259
260 handler->PSendSysMessage(LANG_GUILD_RENAME_DONE, oldGuildStr, newGuildStr);
261 return true;
262 }
263
265 {
266 Guild* guild = nullptr;
267
268 if (guildIdentifier)
269 {
270 if (ObjectGuid::LowType const* guid = std::get_if<ObjectGuid::LowType>(&*guildIdentifier))
271 guild = sGuildMgr->GetGuildById(*guid);
272 else
273 guild = sGuildMgr->GetGuildByName(guildIdentifier->get<std::string_view>());
274 }
275 else if (Optional<PlayerIdentifier> target = PlayerIdentifier::FromTargetOrSelf(handler); target && target->IsConnected())
276 guild = target->GetConnectedPlayer()->GetGuild();
277
278 if (!guild)
279 return false;
280
281 // Display Guild Information
282 handler->PSendSysMessage(LANG_GUILD_INFO_NAME, guild->GetName().c_str(), std::to_string(guild->GetId()).c_str()); // Guild Id + Name
283
284 std::string guildMasterName;
285 if (sCharacterCache->GetCharacterNameByGuid(guild->GetLeaderGUID(), guildMasterName))
286 handler->PSendSysMessage(LANG_GUILD_INFO_GUILD_MASTER, guildMasterName.c_str(), guild->GetLeaderGUID().ToString().c_str()); // Guild Master
287
288 // Format creation date
289 char createdDateStr[20];
290 time_t createdDate = guild->GetCreatedDate();
291 tm localTm;
292 strftime(createdDateStr, 20, "%Y-%m-%d %H:%M:%S", localtime_r(&createdDate, &localTm));
293
294 handler->PSendSysMessage(LANG_GUILD_INFO_CREATION_DATE, createdDateStr); // Creation Date
295 handler->PSendSysMessage(LANG_GUILD_INFO_MEMBER_COUNT, guild->GetMembersCount()); // Number of Members
296 handler->PSendSysMessage(LANG_GUILD_INFO_BANK_GOLD, std::to_string(guild->GetBankMoney() / 100 / 100).c_str()); // Bank Gold (in gold coins)
297 handler->PSendSysMessage(LANG_GUILD_INFO_LEVEL, guild->GetLevel()); // Level
298 handler->PSendSysMessage(LANG_GUILD_INFO_MOTD, guild->GetMOTD().c_str()); // Message of the Day
299 handler->PSendSysMessage(LANG_GUILD_INFO_EXTRA_INFO, guild->GetInfo().c_str()); // Extra Information
300 return true;
301 }
302};
303
305{
307}
#define sCharacterCache
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
uint8_t uint8
Definition: Define.h:144
#define sGuildMgr
Definition: GuildMgr.h:70
GuildRankId
Definition: Guild.h:96
@ LANG_GUILD_NOT_CREATED
Definition: Language.h:569
@ LANG_GUILD_INFO_GUILD_MASTER
Definition: Language.h:951
@ LANG_GUILD_INFO_BANK_GOLD
Definition: Language.h:954
@ LANG_PLAYER_IN_GUILD
Definition: Language.h:568
@ LANG_GUILD_RENAME_DONE
Definition: Language.h:129
@ LANG_COMMAND_COULDNOTFIND
Definition: Language.h:494
@ LANG_GUILD_INFO_EXTRA_INFO
Definition: Language.h:956
@ LANG_GUILD_INFO_MEMBER_COUNT
Definition: Language.h:953
@ LANG_GUILD_RENAME_ALREADY_EXISTS
Definition: Language.h:128
@ LANG_GUILD_INFO_LEVEL
Definition: Language.h:988
@ LANG_BAD_VALUE
Definition: Language.h:149
@ LANG_GUILD_INFO_MOTD
Definition: Language.h:955
@ LANG_GUILD_INFO_NAME
Definition: Language.h:950
@ LANG_INSERT_GUILD_NAME
Definition: Language.h:566
@ LANG_GUILD_INFO_CREATION_DATE
Definition: Language.h:952
#define sObjectMgr
Definition: ObjectMgr.h:1946
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
Role Based Access Control related classes definition.
char * extractQuotedArg(char *args)
Definition: Chat.cpp:546
void PSendSysMessage(const char *fmt, Args &&... args)
Definition: Chat.h:57
void SetSentErrorMessage(bool val)
Definition: Chat.h:114
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition: Chat.cpp:113
bool extractPlayerTarget(char *args, Player **player, ObjectGuid *player_guid=nullptr, std::string *player_name=nullptr)
Definition: Chat.cpp:489
Definition: Guild.h:329
uint64 GetBankMoney() const
Definition: Guild.h:759
ObjectGuid GetLeaderGUID() const
Definition: Guild.h:754
ObjectGuid::LowType GetId() const
Definition: Guild.h:752
uint8 GetLevel() const
Definition: Guild.h:866
bool AddMember(CharacterDatabaseTransaction trans, ObjectGuid guid, Optional< GuildRankId > rankId={})
Definition: Guild.cpp:2813
std::string const & GetName() const
Definition: Guild.h:755
bool SetName(std::string_view name)
Definition: Guild.cpp:1310
bool ChangeMemberRank(CharacterDatabaseTransaction trans, ObjectGuid guid, GuildRankId newRank)
Definition: Guild.cpp:3018
bool Create(Player *pLeader, std::string_view name)
Definition: Guild.cpp:1136
time_t GetCreatedDate() const
Definition: Guild.h:758
bool DeleteMember(CharacterDatabaseTransaction trans, ObjectGuid guid, bool isDisbanding=false, bool isKicked=false)
Definition: Guild.cpp:2955
std::string const & GetInfo() const
Definition: Guild.h:757
void Disband()
Definition: Guild.cpp:1197
std::string const & GetMOTD() const
Definition: Guild.h:756
uint32 GetMembersCount() const
Definition: Guild.h:850
std::string ToString() const
Definition: ObjectGuid.cpp:554
uint64 LowType
Definition: ObjectGuid.h:278
ObjectGuid::LowType GetGuildId() const
Definition: Player.h:1993
std::vector< ChatCommand > GetCommands() const override
Definition: cs_guild.cpp:48
static bool HandleGuildCreateCommand(ChatHandler *handler, char const *args)
GM command level 3 - Create a guild.
Definition: cs_guild.cpp:75
static bool HandleGuildDeleteCommand(ChatHandler *handler, char const *args)
Definition: cs_guild.cpp:130
static bool HandleGuildInfoCommand(ChatHandler *handler, Optional< Variant< ObjectGuid::LowType, std::string_view > > const &guildIdentifier)
Definition: cs_guild.cpp:264
static bool HandleGuildUninviteCommand(ChatHandler *handler, char const *args)
Definition: cs_guild.cpp:177
static bool HandleGuildRenameCommand(ChatHandler *handler, char const *_args)
Definition: cs_guild.cpp:215
static bool HandleGuildRankCommand(ChatHandler *handler, Optional< PlayerIdentifier > player, uint8 rank)
Definition: cs_guild.cpp:197
static bool HandleGuildInviteCommand(ChatHandler *handler, char const *args)
Definition: cs_guild.cpp:149
void AddSC_guild_commandscript()
Definition: cs_guild.cpp:304
@ 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