diff --git a/src/bot/commons.ts b/src/bot/commons.ts index 8dd4112f27d..1ea15ee2403 100644 --- a/src/bot/commons.ts +++ b/src/bot/commons.ts @@ -37,9 +37,9 @@ export async function parserReply(response: string | Promise, 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) { diff --git a/src/bot/customvariables.ts b/src/bot/customvariables.ts index 6efc37f2fca..81b3d6375c7 100644 --- a/src/bot/customvariables.ts +++ b/src/bot/customvariables.ts @@ -45,12 +45,12 @@ class CustomVariables extends Core { }, {}); } - async executeVariablesInText(text: string): Promise { + async executeVariablesInText(text: string, attr: { sender: { userId: number; username: string; source: 'twitch' | 'discord' }} | null): Promise { 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); } @@ -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, @@ -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); } @@ -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; @@ -207,6 +207,7 @@ class CustomVariables extends Core { sender = { username: sender, userId: await users.getIdByName(sender), + source: 'twitch', }; } diff --git a/src/bot/message.ts b/src/bot/message.ts index c84235f1e4d..18da5effd56 100644 --- a/src/bot/message.ts +++ b/src/bot/message.ts @@ -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) => { @@ -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) => { diff --git a/src/bot/registries/text.ts b/src/bot/registries/text.ts index 3579e3af953..5dc4740b07b 100644 --- a/src/bot/registries/text.ts +++ b/src/bot/registries/text.ts @@ -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) { diff --git a/src/bot/systems/alias.ts b/src/bot/systems/alias.ts index aa9162cf61d..89f69b48e06 100644 --- a/src/bot/systems/alias.ts +++ b/src/bot/systems/alias.ts @@ -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); diff --git a/src/panel/views/registries/custom-variables/custom-variables-code.txt b/src/panel/views/registries/custom-variables/custom-variables-code.txt index 483c97f4eab..40e20461f47 100644 --- a/src/panel/views/registries/custom-variables/custom-variables-code.txt +++ b/src/panel/views/registries/custom-variables/custom-variables-code.txt @@ -8,6 +8,7 @@ sender?: { // (only in custom commands, keyword) username: string, userId: number, + source: 'twitch' | 'discord' } random: { online: {