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 "CharacterCache.h"
27#include "Chat.h"
28#include "Guild.h"
29#include "GuildMgr.h"
30#include "Language.h"
31#include "ObjectAccessor.h"
32#include "ObjectMgr.h"
33#include "Player.h"
34#include "RBAC.h"
35
36#if TRINITY_COMPILER == TRINITY_COMPILER_GNU
37#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
38#endif
39
40using namespace Trinity::ChatCommands;
42{
43public:
44 guild_commandscript() : CommandScript("guild_commandscript") { }
45
46 std::vector<ChatCommand> GetCommands() const override
47 {
48 static std::vector<ChatCommand> guildCommandTable =
49 {
57 };
58 static std::vector<ChatCommand> commandTable =
59 {
60 { "guild", rbac::RBAC_PERM_COMMAND_GUILD, true, nullptr, "", guildCommandTable },
61 };
62 return commandTable;
63 }
64
73 static bool HandleGuildCreateCommand(ChatHandler* handler, char const* args)
74 {
75 if (!*args)
76 return false;
77
78 // if not guild name only (in "") then player name
79 Player* target;
80 if (!handler->extractPlayerTarget(*args != '"' ? (char*)args : nullptr, &target))
81 return false;
82
83 char* tailStr = *args != '"' ? strtok(nullptr, "") : (char*)args;
84 if (!tailStr)
85 return false;
86
87 char* guildStr = handler->extractQuotedArg(tailStr);
88 if (!guildStr)
89 return false;
90
91 std::string guildName = guildStr;
92
93 if (target->GetGuildId())
94 {
96 handler->SetSentErrorMessage(true);
97 return false;
98 }
99
100 if (sGuildMgr->GetGuildByName(guildName))
101 {
103 handler->SetSentErrorMessage(true);
104 return false;
105 }
106
107 if (sObjectMgr->IsReservedName(guildName) || !sObjectMgr->IsValidCharterName(guildName))
108 {
110 handler->SetSentErrorMessage(true);
111 return false;
112 }
113
114 Guild* guild = new Guild;
115 if (!guild->Create(target, guildName))
116 {
117 delete guild;
119 handler->SetSentErrorMessage(true);
120 return false;
121 }
122
123 sGuildMgr->AddGuild(guild);
124
125 return true;
126 }
127
128 static bool HandleGuildDeleteCommand(ChatHandler* handler, char const* args)
129 {
130 if (!*args)
131 return false;
132
133 char* guildStr = handler->extractQuotedArg((char*)args);
134 if (!guildStr)
135 return false;
136
137 std::string guildName = guildStr;
138
139 Guild* targetGuild = sGuildMgr->GetGuildByName(guildName);
140 if (!targetGuild)
141 return false;
142
143 targetGuild->Disband();
144 return true;
145 }
146
147 static bool HandleGuildInviteCommand(ChatHandler* handler, char const* args)
148 {
149 if (!*args)
150 return false;
151
152 // if not guild name only (in "") then player name
153 ObjectGuid targetGuid;
154 if (!handler->extractPlayerTarget(*args != '"' ? (char*)args : nullptr, nullptr, &targetGuid))
155 return false;
156
157 char* tailStr = *args != '"' ? strtok(nullptr, "") : (char*)args;
158 if (!tailStr)
159 return false;
160
161 char* guildStr = handler->extractQuotedArg(tailStr);
162 if (!guildStr)
163 return false;
164
165 std::string guildName = guildStr;
166 Guild* targetGuild = sGuildMgr->GetGuildByName(guildName);
167 if (!targetGuild)
168 return false;
169
170 // player's guild membership checked in AddMember before add
171 CharacterDatabaseTransaction trans(nullptr);
172 return targetGuild->AddMember(trans, targetGuid);
173 }
174
175 static bool HandleGuildUninviteCommand(ChatHandler* handler, char const* args)
176 {
177 Player* target;
178 ObjectGuid targetGuid;
179 if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid))
180 return false;
181
182 ObjectGuid::LowType guildId = target ? target->GetGuildId() : sCharacterCache->GetCharacterGuildIdByGuid(targetGuid);
183 if (!guildId)
184 return false;
185
186 Guild* targetGuild = sGuildMgr->GetGuildById(guildId);
187 if (!targetGuild)
188 return false;
189
190 CharacterDatabaseTransaction trans(nullptr);
191 targetGuild->DeleteMember(trans, targetGuid, false, true);
192 return true;
193 }
194
196 {
197 if (!player)
198 player = PlayerIdentifier::FromTargetOrSelf(handler);
199 if (!player)
200 return false;
201
202 ObjectGuid::LowType guildId = player->IsConnected() ? player->GetConnectedPlayer()->GetGuildId() : sCharacterCache->GetCharacterGuildIdByGuid(*player);
203 if (!guildId)
204 return false;
205
206 Guild* targetGuild = sGuildMgr->GetGuildById(guildId);
207 if (!targetGuild)
208 return false;
209
210 return targetGuild->ChangeMemberRank(nullptr, *player, rank);
211 }
212
213 static bool HandleGuildRenameCommand(ChatHandler* handler, char const* _args)
214 {
215 if (!*_args)
216 return false;
217
218 char *args = (char *)_args;
219
220 char const* oldGuildStr = handler->extractQuotedArg(args);
221 if (!oldGuildStr)
222 {
224 handler->SetSentErrorMessage(true);
225 return false;
226 }
227
228 char const* newGuildStr = handler->extractQuotedArg(strtok(nullptr, ""));
229 if (!newGuildStr)
230 {
232 handler->SetSentErrorMessage(true);
233 return false;
234 }
235
236 Guild* guild = sGuildMgr->GetGuildByName(oldGuildStr);
237 if (!guild)
238 {
239 handler->PSendSysMessage(LANG_COMMAND_COULDNOTFIND, oldGuildStr);
240 handler->SetSentErrorMessage(true);
241 return false;
242 }
243
244 if (sGuildMgr->GetGuildByName(newGuildStr))
245 {
247 handler->SetSentErrorMessage(true);
248 return false;
249 }
250
251 if (!guild->SetName(newGuildStr))
252 {
254 handler->SetSentErrorMessage(true);
255 return false;
256 }
257
258 handler->PSendSysMessage(LANG_GUILD_RENAME_DONE, oldGuildStr, newGuildStr);
259 return true;
260 }
261
263 {
264 Guild* guild = nullptr;
265
266 if (guildIdentifier)
267 {
268 if (ObjectGuid::LowType const* guid = std::get_if<ObjectGuid::LowType>(&*guildIdentifier))
269 guild = sGuildMgr->GetGuildById(*guid);
270 else
271 guild = sGuildMgr->GetGuildByName(guildIdentifier->get<std::string_view>());
272 }
273 else if (Optional<PlayerIdentifier> target = PlayerIdentifier::FromTargetOrSelf(handler); target && target->IsConnected())
274 guild = target->GetConnectedPlayer()->GetGuild();
275
276 if (!guild)
277 return false;
278
279 // Display Guild Information
280 handler->PSendSysMessage(LANG_GUILD_INFO_NAME, guild->GetName().c_str(), guild->GetId()); // Guild Id + Name
281
282 std::string guildMasterName;
283 if (sCharacterCache->GetCharacterNameByGuid(guild->GetLeaderGUID(), guildMasterName))
284 handler->PSendSysMessage(LANG_GUILD_INFO_GUILD_MASTER, guildMasterName.c_str(), guild->GetLeaderGUID().ToString().c_str()); // Guild Master
285
286 // Format creation date
287 char createdDateStr[20];
288 time_t createdDate = guild->GetCreatedDate();
289 tm localTm;
290 strftime(createdDateStr, 20, "%Y-%m-%d %H:%M:%S", localtime_r(&createdDate, &localTm));
291
292 handler->PSendSysMessage(LANG_GUILD_INFO_CREATION_DATE, createdDateStr); // Creation Date
293 handler->PSendSysMessage(LANG_GUILD_INFO_MEMBER_COUNT, guild->GetMemberCount()); // Number of Members
294 handler->PSendSysMessage(LANG_GUILD_INFO_BANK_GOLD, guild->GetBankMoney() / 100 / 100); // Bank Gold (in gold coins)
295 handler->PSendSysMessage(LANG_GUILD_INFO_MOTD, guild->GetMOTD().c_str()); // Message of the Day
296 handler->PSendSysMessage(LANG_GUILD_INFO_EXTRA_INFO, guild->GetInfo().c_str()); // Extra Information
297 return true;
298 }
299};
300
#define sCharacterCache
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
uint8_t uint8
Definition Define.h:135
#define sGuildMgr
Definition GuildMgr.h:59
@ 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_COMMAND_COULDNOTFIND
Definition Language.h:497
@ 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_BAD_VALUE
Definition Language.h:149
@ LANG_GUILD_INFO_MOTD
Definition Language.h:958
@ LANG_GUILD_INFO_NAME
Definition Language.h:953
@ LANG_INSERT_GUILD_NAME
Definition Language.h:569
@ LANG_GUILD_INFO_CREATION_DATE
Definition Language.h:955
#define sObjectMgr
Definition ObjectMgr.h:1721
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:646
void SetSentErrorMessage(bool val)
Definition Chat.h:134
void PSendSysMessage(char const *fmt, Args &&... args)
Definition Chat.h:69
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition Chat.cpp:101
bool extractPlayerTarget(char *args, Player **player, ObjectGuid *player_guid=nullptr, std::string *player_name=nullptr)
Definition Chat.cpp:589
Definition Guild.h:284
uint64 GetBankMoney() const
Definition Guild.h:645
bool ChangeMemberRank(CharacterDatabaseTransaction trans, ObjectGuid guid, uint8 newRank)
Definition Guild.cpp:2333
ObjectGuid GetLeaderGUID() const
Definition Guild.h:639
bool AddMember(CharacterDatabaseTransaction trans, ObjectGuid guid, uint8 rankId=GUILD_RANK_NONE)
Definition Guild.cpp:2187
ObjectGuid::LowType GetId() const
Definition Guild.h:638
uint32 GetMemberCount() const
Definition Guild.h:643
std::string const & GetName() const
Definition Guild.h:640
bool SetName(std::string_view name)
Definition Guild.cpp:1222
bool Create(Player *pLeader, std::string_view name)
Definition Guild.cpp:1087
time_t GetCreatedDate() const
Definition Guild.h:644
bool DeleteMember(CharacterDatabaseTransaction trans, ObjectGuid guid, bool isDisbanding=false, bool isKicked=false)
Definition Guild.cpp:2269
std::string const & GetInfo() const
Definition Guild.h:642
void Disband()
Definition Guild.cpp:1142
std::string const & GetMOTD() const
Definition Guild.h:641
std::string ToString() const
uint32 LowType
Definition ObjectGuid.h:142
ObjectGuid::LowType GetGuildId() const
Definition Player.h:1620
std::vector< ChatCommand > GetCommands() const override
Definition cs_guild.cpp:46
static bool HandleGuildCreateCommand(ChatHandler *handler, char const *args)
GM command level 3 - Create a guild.
Definition cs_guild.cpp:73
static bool HandleGuildDeleteCommand(ChatHandler *handler, char const *args)
Definition cs_guild.cpp:128
static bool HandleGuildInfoCommand(ChatHandler *handler, Optional< Variant< ObjectGuid::LowType, std::string_view > > const &guildIdentifier)
Definition cs_guild.cpp:262
static bool HandleGuildUninviteCommand(ChatHandler *handler, char const *args)
Definition cs_guild.cpp:175
static bool HandleGuildRenameCommand(ChatHandler *handler, char const *_args)
Definition cs_guild.cpp:213
static bool HandleGuildRankCommand(ChatHandler *handler, Optional< PlayerIdentifier > player, uint8 rank)
Definition cs_guild.cpp:195
static bool HandleGuildInviteCommand(ChatHandler *handler, char const *args)
Definition cs_guild.cpp:147
void AddSC_guild_commandscript()
Definition cs_guild.cpp:301
@ RBAC_PERM_COMMAND_GUILD_UNINVITE
Definition RBAC.h:275
@ RBAC_PERM_COMMAND_GUILD_INVITE
Definition RBAC.h:274
@ RBAC_PERM_COMMAND_GUILD
Definition RBAC.h:271
@ RBAC_PERM_COMMAND_GUILD_RANK
Definition RBAC.h:276
@ RBAC_PERM_COMMAND_GUILD_INFO
Definition RBAC.h:664
@ RBAC_PERM_COMMAND_GUILD_RENAME
Definition RBAC.h:277
@ RBAC_PERM_COMMAND_GUILD_CREATE
Definition RBAC.h:272
@ RBAC_PERM_COMMAND_GUILD_DELETE
Definition RBAC.h:273
static Optional< PlayerIdentifier > FromTargetOrSelf(ChatHandler *handler)