Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update name command #1789

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/classes/CommandParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export default class CommandParser {

static parseParams(paramString: string): UnknownDictionaryKnownValues {
const params: UnknownDictionaryKnownValues = parseJSON(
'{"' + paramString.replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}'
'{"' +
paramString
.replace(/"/g, '\\"')
.replace(/&(?!&)(?=[^&=]+=[^&]*)/g, '","') // Split only valid key-value pairs
.replace(/=/g, '":"') +
'"}'
);

const parsed: UnknownDictionaryKnownValues = {};
Expand Down
2 changes: 2 additions & 0 deletions src/classes/Commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ export default class Commands {
this.manager.TF2GCCommand(steamID, message, command as TF2GC);
} else if (['name', 'avatar'].includes(command) && isAdmin) {
this.manager.nameAvatarCommand(steamID, message, command as NameAvatar, prefix);
} else if (command === 'changename' && isAdmin) {
this.manager.changeNameCommand(steamID, message, prefix);
} else if (['block', 'unblock'].includes(command) && isAdmin) {
this.manager.blockUnblockCommand(steamID, message, command as BlockUnblock);
} else if (['blockedlist', 'blocklist', 'blist'].includes(command) && isAdmin) {
Expand Down
2 changes: 1 addition & 1 deletion src/classes/Commands/sub-classes/Help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default class HelpCommands {
`haltstatus - Get the info whether the bot is paused or not ⏯`,
`refreshautokeys - Refresh the bot's autokeys settings `,
`refreshlist - Refresh sell listings 🔄`,
`name <new_name> - Change the bot's name `,
`changeName name=<new_name> - Change the bot's name `,
`avatar <image_URL> - Change the bot's avatar `,
`donatebptf (sku|name|defindex)=<a>&amount=<integer> - Donate to backpack.tf (https://backpack.tf/donate) 💰`,
`premium months=<integer> - Purchase backpack.tf premium using keys (https://backpack.tf/premium/subscribe) 👑`,
Expand Down
65 changes: 44 additions & 21 deletions src/classes/Commands/sub-classes/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,30 +245,18 @@ export default class ManagerCommands {
'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f5/f57685d33224e32436f366d1acb4a1769bdfa60f_full.jpg';
const input = CommandParser.removeCommand(message);

if (!input || input === `!${command}`) {
return this.bot.sendMessage(
steamID,
`❌ You forgot to add ${command === 'name' ? 'a name' : 'an image url'}. Example: "!${
command === 'name' ? 'name IdiNium' : `avatar ${example}`
} "`
);
}

if (command === 'name') {
this.bot.community.editProfile(
{
name: input
},
err => {
if (err) {
log.warn('Error while changing name: ', err);
return this.bot.sendMessage(steamID, `❌ Error while changing name: ${err.message}`);
}

this.bot.sendMessage(steamID, '✅ Successfully changed name.');
}
this.bot.sendMessage(
steamID,
`The ${prefix}name command has been updated to ${prefix}changeName. Please use the new command going forward!`
);
} else {
if (!input || input === `!${command}`) {
return this.bot.sendMessage(
steamID,
`❌ You forgot to add an image url'. Example: "!${`avatar ${example}`} "`
);
}
if (!validUrl.isUri(input)) {
return this.bot.sendMessage(steamID, `❌ Your url is not valid. Example: "${prefix}avatar ${example}"`);
}
Expand All @@ -284,6 +272,41 @@ export default class ManagerCommands {
}
}

changeNameCommand(steamID: SteamID, message: string, prefix: string): void {
const params = CommandParser.parseParams(CommandParser.removeCommand(message));
const inputName = params.name as string;

if (inputName !== undefined) {
if (params.i_am_sure !== 'yes_i_am') {
return this.bot.sendMessage(
steamID,
`⚠️ Are you sure that you want to change your bot's name?` +
`\nChanging the name will result in a trading cooldown for a few hours on your bot's account` +
`\nIf yes, retry by sending ${prefix}changeName name=${inputName}&i_am_sure=yes_i_am`
);
} else {
this.bot.community.editProfile(
{
name: inputName
},
err => {
if (err) {
log.warn('Error while changing name: ', err);
return this.bot.sendMessage(steamID, `❌ Error while changing name: ${err.message}`);
}

this.bot.sendMessage(steamID, '✅ Successfully changed name.');
}
);
}
} else {
return this.bot.sendMessage(
steamID,
`⚠️ Missing name property. Example: "${prefix}changeName name=IdiNium`
);
}
}

blockedListCommand(steamID: SteamID): void {
if (this.isSendingBlockedList) {
return;
Expand Down
14 changes: 14 additions & 0 deletions src/classes/__tests__/CommandParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,17 @@ it('can parse item id commands', () => {
const params = CommandParser.parseParams(CommandParser.removeCommand(message));
expect(params).toEqual({ id: 10151297782, intent: 'sell', sell: { keys: 1 } });
});

it('can parse values with &', () => {
let message = '!changeName name=Buying spells & hats&i_am_sure=i_am_sure';
let params = CommandParser.parseParams(CommandParser.removeCommand(message));
expect(params).toEqual({ name: 'Buying spells & hats', i_am_sure: 'i_am_sure' });

message = `!test key1=value1&key2=value2&&key3=3`;
params = CommandParser.parseParams(CommandParser.removeCommand(message));
expect(params).toEqual({ key1: 'value1', key2: 'value2&', key3: 3 });

message = `!test key1=value1&key2=&value2&&key3=[value3]`;
params = CommandParser.parseParams(CommandParser.removeCommand(message));
expect(params).toEqual({ key1: 'value1', key2: '&value2&', key3: ['value3'] });
});
Loading