Moved chatbot user instance out of the Sock Chat protocol implementation.
This commit is contained in:
parent
0f8e373225
commit
5413b918f9
3 changed files with 68 additions and 63 deletions
|
@ -1,28 +1,34 @@
|
||||||
Umi.Message = function(msgId, time, user, text, channel, highlight, botInfo, isAction, isLog) {
|
#include user.js
|
||||||
msgId = (msgId || '').toString();
|
|
||||||
time = time === null ? new Date() : new Date(parseInt(time || 0) * 1000);
|
|
||||||
user = user || {};
|
|
||||||
text = (text || '').toString();
|
|
||||||
channel = (channel || '').toString();
|
|
||||||
highlight = !!highlight;
|
|
||||||
isAction = !!isAction;
|
|
||||||
isLog = !!isLog;
|
|
||||||
hasSeen = isLog;
|
|
||||||
|
|
||||||
const msgIdInt = parseInt(msgId);
|
Umi.Message = (() => {
|
||||||
|
const chatBot = new Umi.User('-1', 'Server');
|
||||||
|
|
||||||
return {
|
return function(msgId, time, user, text, channel, highlight, botInfo, isAction, isLog) {
|
||||||
getId: () => msgId,
|
msgId = (msgId || '').toString();
|
||||||
getIdInt: () => msgIdInt,
|
time = time === null ? new Date() : new Date(parseInt(time || 0) * 1000);
|
||||||
getTime: () => time,
|
user = user !== null && typeof user === 'object' ? user : chatBot;
|
||||||
getUser: () => user,
|
text = (text || '').toString();
|
||||||
getText: () => text,
|
channel = (channel || '').toString();
|
||||||
getChannel: () => channel,
|
highlight = !!highlight;
|
||||||
shouldHighlight: () => highlight,
|
isAction = !!isAction;
|
||||||
getBotInfo: () => botInfo,
|
isLog = !!isLog;
|
||||||
isAction: () => isAction,
|
hasSeen = isLog;
|
||||||
isLog: () => isLog,
|
|
||||||
hasSeen: () => hasSeen,
|
const msgIdInt = parseInt(msgId);
|
||||||
markSeen: () => hasSeen = true,
|
|
||||||
|
return {
|
||||||
|
getId: () => msgId,
|
||||||
|
getIdInt: () => msgIdInt,
|
||||||
|
getTime: () => time,
|
||||||
|
getUser: () => user,
|
||||||
|
getText: () => text,
|
||||||
|
getChannel: () => channel,
|
||||||
|
shouldHighlight: () => highlight,
|
||||||
|
getBotInfo: () => botInfo,
|
||||||
|
isAction: () => isAction,
|
||||||
|
isLog: () => isLog,
|
||||||
|
hasSeen: () => hasSeen,
|
||||||
|
markSeen: () => hasSeen = true,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
})();
|
||||||
|
|
|
@ -19,8 +19,6 @@
|
||||||
Umi.Protocol.SockChat.Protocol = function(views, settings) {
|
Umi.Protocol.SockChat.Protocol = function(views, settings) {
|
||||||
const pub = {};
|
const pub = {};
|
||||||
|
|
||||||
const chatBot = new Umi.User('-1', 'Server');
|
|
||||||
|
|
||||||
let noReconnect = false,
|
let noReconnect = false,
|
||||||
connectAttempts = 0,
|
connectAttempts = 0,
|
||||||
wasKicked = false,
|
wasKicked = false,
|
||||||
|
@ -270,20 +268,20 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Umi.Messages.Add(new Umi.Message(
|
Umi.Messages.Add(new Umi.Message(
|
||||||
data[6], data[1], chatBot, '', channelName, false,
|
data[6], data[1], undefined, '', channelName, false,
|
||||||
{ type: 'join', isError: false, args: [juser.getName()], target: juser }
|
{ type: 'join', isError: false, args: [juser.getName()], target: juser }
|
||||||
));
|
));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '2': // message
|
case '2': // message
|
||||||
let text = data[3];
|
let text = data[3];
|
||||||
const muser = Umi.Users.Get(data[2]) || chatBot,
|
const muser = Umi.Users.Get(data[2]);
|
||||||
textParts = text.split("\f");
|
const textParts = text.split("\f");
|
||||||
isPM = data[5][4] !== '0',
|
const isPM = data[5][4] !== '0';
|
||||||
isAction = data[5][1] !== '0' && data[5][3] === '0',
|
const isAction = data[5][1] !== '0' && data[5][3] === '0';
|
||||||
isBot = data[2] === '-1',
|
const isBot = data[2] === '-1';
|
||||||
pmChannel = '@' + muser.getName(),
|
const botInfo = {};
|
||||||
botInfo = {};
|
let pmChannel = '';
|
||||||
|
|
||||||
text = unfuckText(text);
|
text = unfuckText(text);
|
||||||
|
|
||||||
|
@ -296,9 +294,10 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) {
|
||||||
if(isPM) {
|
if(isPM) {
|
||||||
if(muser.getId() === userId) {
|
if(muser.getId() === userId) {
|
||||||
const tmpMsg = text.split(' ');
|
const tmpMsg = text.split(' ');
|
||||||
pmChannel = '@' + tmpMsg.shift();
|
pmChannel = `@${tmpMsg.shift()}`;
|
||||||
text = tmpMsg.join(' ');
|
text = tmpMsg.join(' ');
|
||||||
}
|
} else
|
||||||
|
pmChannel = `@${muser.getName()}`;
|
||||||
|
|
||||||
if(Umi.Channels.Get(pmChannel) === null)
|
if(Umi.Channels.Get(pmChannel) === null)
|
||||||
Umi.Channels.Add(new Umi.Channel(pmChannel, false, true, true));
|
Umi.Channels.Add(new Umi.Channel(pmChannel, false, true, true));
|
||||||
|
@ -308,7 +307,7 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) {
|
||||||
|
|
||||||
Umi.Messages.Add(new Umi.Message(
|
Umi.Messages.Add(new Umi.Message(
|
||||||
data[4], data[1], muser, text,
|
data[4], data[1], muser, text,
|
||||||
isPM ? pmChannel : (data[6] || channelName),
|
isPM ? pmChannel : channelName,
|
||||||
false, botInfo, isAction
|
false, botInfo, isAction
|
||||||
));
|
));
|
||||||
break;
|
break;
|
||||||
|
@ -317,7 +316,7 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) {
|
||||||
const luser = Umi.Users.Get(data[1]);
|
const luser = Umi.Users.Get(data[1]);
|
||||||
|
|
||||||
Umi.Messages.Add(new Umi.Message(
|
Umi.Messages.Add(new Umi.Message(
|
||||||
data[5], data[4], chatBot, '', channelName, false,
|
data[5], data[4], undefined, '', channelName, false,
|
||||||
{ type: data[3], isError: false, args: [luser.getName()], target: luser }
|
{ type: data[3], isError: false, args: [luser.getName()], target: luser }
|
||||||
));
|
));
|
||||||
Umi.Users.Remove(luser);
|
Umi.Users.Remove(luser);
|
||||||
|
@ -350,7 +349,7 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) {
|
||||||
|
|
||||||
Umi.Users.Add(umuser);
|
Umi.Users.Add(umuser);
|
||||||
Umi.Messages.Add(new Umi.Message(
|
Umi.Messages.Add(new Umi.Message(
|
||||||
data[6], null, chatBot, '', channelName, false,
|
data[6], null, undefined, '', channelName, false,
|
||||||
{ type: 'jchan', isError: false, args: [ umuser.getName() ], target: umuser }
|
{ type: 'jchan', isError: false, args: [ umuser.getName() ], target: umuser }
|
||||||
));
|
));
|
||||||
break;
|
break;
|
||||||
|
@ -362,7 +361,7 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) {
|
||||||
const mouser = Umi.Users.Get(+data[2]);
|
const mouser = Umi.Users.Get(+data[2]);
|
||||||
|
|
||||||
Umi.Messages.Add(new Umi.Message(
|
Umi.Messages.Add(new Umi.Message(
|
||||||
data[3], null, chatBot, '', channelName, false,
|
data[3], null, undefined, '', channelName, false,
|
||||||
{ type: 'lchan', isError: false, args: [ mouser.getName() ], target: mouser }
|
{ type: 'lchan', isError: false, args: [ mouser.getName() ], target: mouser }
|
||||||
));
|
));
|
||||||
Umi.Users.Remove(mouser);
|
Umi.Users.Remove(mouser);
|
||||||
|
|
|
@ -4,12 +4,12 @@ Umi.User = function(userId, userName, userColour, userPerms) {
|
||||||
|
|
||||||
const userIdInt = parseInt(userId);
|
const userIdInt = parseInt(userId);
|
||||||
|
|
||||||
const setName = function(name) {
|
const setName = name => {
|
||||||
userName = (name || '').toString().replace('<', '<').replace('>', '>');
|
userName = (name || '').toString().replace('<', '<').replace('>', '>');
|
||||||
};
|
};
|
||||||
setName(userName);
|
setName(userName);
|
||||||
|
|
||||||
const setColour = function(colour) {
|
const setColour = colour => {
|
||||||
userColour = (colour || 'inherit').toString();
|
userColour = (colour || 'inherit').toString();
|
||||||
};
|
};
|
||||||
setColour(userColour);
|
setColour(userColour);
|
||||||
|
@ -33,34 +33,34 @@ Umi.User = function(userId, userName, userColour, userPerms) {
|
||||||
let avatarTime = Date.now();
|
let avatarTime = Date.now();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getId: function() { return userId; },
|
getId: () => userId,
|
||||||
getIdInt: function() { return userIdInt; },
|
getIdInt: () => userIdInt,
|
||||||
|
|
||||||
getName: function() { return userName; },
|
getName: () => userName,
|
||||||
setName: setName,
|
setName: setName,
|
||||||
|
|
||||||
getColour: function() { return userColour; },
|
getColour: () => userColour,
|
||||||
setColour: setColour,
|
setColour: setColour,
|
||||||
|
|
||||||
setPermissions: setPerms,
|
setPermissions: setPerms,
|
||||||
|
|
||||||
getRank: function() { return userRank; },
|
getRank: () => userRank,
|
||||||
isCurrentUser: function() { return Umi.User.currentUser && Umi.User.currentUser.userId === userId; },
|
isCurrentUser: () => Umi.User.currentUser && Umi.User.currentUser.userId === userId,
|
||||||
canBan: function() { return canBan; },
|
canBan: () => canBan,
|
||||||
canSilence: function() { return canBan; },
|
canSilence: () => canBan,
|
||||||
canCreateChannel: function() { return canCreateChannel; },
|
canCreateChannel: () => canCreateChannel,
|
||||||
canSetNickName: function() { return canSetNickName; },
|
canSetNickName: () => canSetNickName,
|
||||||
getAvatarTime: function() { return avatarTime; },
|
getAvatarTime: () => avatarTime,
|
||||||
bumpAvatarTime: function() { avatarTime = Date.now(); },
|
bumpAvatarTime: () => { avatarTime = Date.now(); },
|
||||||
|
|
||||||
isBot: function() { return userId === '-1'; },
|
isBot: () => userId === '-1',
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
Umi.User.currentUser = undefined;
|
Umi.User.currentUser = undefined;
|
||||||
Umi.User.hasCurrentUser = function() { return Umi.User.currentUser !== undefined; };
|
Umi.User.hasCurrentUser = () => Umi.User.currentUser !== undefined;
|
||||||
Umi.User.getCurrentUser = function() { return Umi.User.currentUser; };
|
Umi.User.getCurrentUser = () => Umi.User.currentUser;
|
||||||
Umi.User.setCurrentUser = function(user) { Umi.User.currentUser = user; };
|
Umi.User.setCurrentUser = user => { Umi.User.currentUser = user; };
|
||||||
Umi.User.isCurrentUser = function(user) {
|
Umi.User.isCurrentUser = user => user !== null && typeof user === 'object' && typeof user.getId === 'function'
|
||||||
return Umi.User.currentUser !== undefined
|
&& Umi.User.currentUser !== null && typeof Umi.User.currentUser === 'object'
|
||||||
&& (Umi.User.currentUser === user || Umi.User.currentUser.getId() === user.getId());
|
&& typeof Umi.User.currentUser.getId === 'function'
|
||||||
};
|
&& (Umi.User.currentUser === user || Umi.User.currentUser.getId() === user.getId());
|
||||||
|
|
Loading…
Reference in a new issue