Skip to content

Commit

Permalink
feat(customvariables): add source attribute to sender (#3919)
Browse files Browse the repository at this point in the history
Source attribute can contain twitch or discord value dependant on
source of user command

Fixes https://community.sogebot.xyz/t/get-custom-variable-script-trigger-source/128
  • Loading branch information
sogehige committed Jun 20, 2020
1 parent 2c36d52 commit 72f5778
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/bot/commons.ts
Expand Up @@ -37,9 +37,9 @@ export async function parserReply(response: string | Promise<string>, opts: { se
};
const messageToSend = await (async () => {
if (opts.attr?.skip) {
return prepare(await response as string, { ...opts, sender: senderObject.discord ? senderObject.discord.author : senderObject }, false);
return prepare(await response as string, { ...opts, sender: senderObject.discord ? { ...senderObject, discord: senderObject.discord.author } : senderObject }, false);
} else {
return await new Message(await response as string).parse({ ...opts, sender: senderObject.discord ? senderObject.discord.author : senderObject }) as string;
return await new Message(await response as string).parse({ ...opts, sender: senderObject.discord ? { ...senderObject, discord: senderObject.discord.author } : senderObject }) as string;
}
})();
if (opts.sender.discord) {
Expand Down
11 changes: 6 additions & 5 deletions src/bot/customvariables.ts
Expand Up @@ -45,12 +45,12 @@ class CustomVariables extends Core {
}, {});
}

async executeVariablesInText(text: string): Promise<string> {
async executeVariablesInText(text: string, attr: { sender: { userId: number; username: string; source: 'twitch' | 'discord' }} | null): Promise<string> {
for (const variable of text.match(customVariableRegex) || []) {
const isVariable = await this.isVariableSet(variable);
let value = '';
if (isVariable) {
value = await this.getValueOf(variable) || '';
value = await this.getValueOf(variable, attr) || '';
}
text = text.replace(new RegExp(`\\${variable}`, 'g'), value);
}
Expand Down Expand Up @@ -128,7 +128,7 @@ class CustomVariables extends Core {
if (!item) {
throw new Error('Variable not found');
}
const newCurrentValue = await this.runScript(item.evalValue, { sender: null,_current: item.currentValue, isUI: true });
const newCurrentValue = await this.runScript(item.evalValue, { sender: null, _current: item.currentValue, isUI: true });
const runAt = Date.now();
cb(null, await getRepository(Variable).save({
...item, currentValue: newCurrentValue, runAt,
Expand All @@ -140,7 +140,7 @@ class CustomVariables extends Core {
adminEndpoint(this.nsp, 'customvariables::testScript', async (opts, cb) => {
let returnedValue;
try {
returnedValue = await this.runScript(opts.evalValue, { isUI: true, _current: opts.currentValue, sender: { username: 'testuser', userId: 0 }});
returnedValue = await this.runScript(opts.evalValue, { isUI: true, _current: opts.currentValue, sender: { username: 'testuser', userId: 0, source: 'twitch' }});
} catch (e) {
cb(e.stack, null);
}
Expand Down Expand Up @@ -198,7 +198,7 @@ class CustomVariables extends Core {
});
}

async runScript (script: string, opts: { sender: { userId: number; username: string } | string | null, isUI: boolean; param?: string | number, _current: any }) {
async runScript (script: string, opts: { sender: { userId: number; username: string; source: 'twitch' | 'discord' } | string | null, isUI: boolean; param?: string | number, _current: any }) {
debug('customvariables.eval', opts);
let sender = !isNil(opts.sender) ? opts.sender : null;
const isUI = !isNil(opts.isUI) ? opts.isUI : false;
Expand All @@ -207,6 +207,7 @@ class CustomVariables extends Core {
sender = {
username: sender,
userId: await users.getIdByName(sender),
source: 'twitch',
};
}

Expand Down
10 changes: 8 additions & 2 deletions src/bot/message.ts
Expand Up @@ -282,7 +282,10 @@ class Message {
return state.isOk && !state.isEval ? state.setValue : state.updated.currentValue;
}
}
return customvariables.getValueOf(variable, { sender: attr.sender, param: attr.param });
return customvariables.getValueOf(variable, {
sender: { ...attr.sender, source: typeof attr.sender.discord === 'undefined' ? 'twitch' : 'discord' },
param: attr.param,
});
},
// force quiet variable set
'$!_#': async (variable: string) => {
Expand All @@ -291,7 +294,10 @@ class Message {
const state = await customvariables.setValueOf(variable, attr.param, { sender: attr.sender });
return state.updated.currentValue;
}
return customvariables.getValueOf(variable, { sender: attr.sender, param: attr.param });
return customvariables.getValueOf(variable, {
sender: { ...attr.sender, source: typeof attr.sender.discord === 'undefined' ? 'twitch' : 'discord' },
param: attr.param,
});
},
// force full quiet variable
'$!!_#': async (variable: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/bot/registries/text.ts
Expand Up @@ -46,7 +46,7 @@ class Text extends Registry {
const item = await getRepository(TextEntity).findOneOrFail({ id: opts.id });
let text = item.text;
if (opts.parseText) {
text = await new Message(await customvariables.executeVariablesInText(text)).parse();
text = await new Message(await customvariables.executeVariablesInText(text, null)).parse();
}
callback(null, {...item, text});
} catch(e) {
Expand Down
9 changes: 8 additions & 1 deletion src/bot/systems/alias.ts
Expand Up @@ -124,7 +124,14 @@ class Alias extends System {
}
if (getFromViewersCache(opts.sender.userId, alias.permission)) {
// process custom variables
const response = await customvariables.executeVariablesInText(opts.message.replace(replace, alias.command));
const response = await customvariables.executeVariablesInText(
opts.message.replace(replace, alias.command), {
sender: {
userId: opts.sender.userId,
username: opts.sender.username,
source: typeof opts.sender.discord === 'undefined' ? 'twitch' : 'discord',
},
});
debug('alias.process', response);
const responses = await p.command(opts.sender, response, true);
debug('alias.process', responses);
Expand Down
Expand Up @@ -8,6 +8,7 @@
sender?: { // (only in custom commands, keyword)
username: string,
userId: number,
source: 'twitch' | 'discord'
}
random: {
online: {
Expand Down

0 comments on commit 72f5778

Please sign in to comment.