Skip to content

ExampleCode.2.1

Luckshya edited this page Jul 9, 2020 · 1 revision
sessions <- {};

class MyDiscord
{
	eventFuncs = null;
	session = null;
	connID	= null;
	s_Servers = null;
	s_OtherChannels = null;
	
	function constructor()
	{
		session = SqDiscord.CSession();
		connID = session.ConnID;
		
		s_Servers = {};
		s_OtherChannels = {};
			
		sessions.rawset(connID, this);
		
		eventFuncs = [
			onReady,
			onMessage,
			onError,
			onDisconnect,
			onQuit,
			onGuild,
			onGuildEdit,
			onGuildDelete,
			onMember,
			onMemberEdit,
			onMemberRemove,
			onChannel,
			onChannelEdit,
			onChannelDelete,
			onRole,
			onRoleEdit,
			onRoleDelete,
			onUserEdit
		];
		
		session.InternalCacheEnabled = false;
	}
	
	function Connect(token)
	{
		session.Connect(token);
	}
	
	function sendMessage(channelID, message)
	{
		session.Message(channelID, message);
	}
	
	function sendEmbed(channelID, embed)
	{
		session.MessageEmbed(channelID, "", embed);
	}
	
	function onReady() {
		print("Discord bot connection established successfully.");
	}
	
	function onMessage(message) {
		print("OnMessage");
		
		local member = message.Member;
		local serverID = message.ServerID;
		
		if(member != null && serverID != null && s_Servers.rawin(serverID)) {
			local server = s_Servers[serverID];
			
			foreach(roleID in member.Roles) {
				local roleInfo = server.Roles[roleID];
				print(format("%s - %d", roleInfo.Name, roleInfo.Position));
			}
		}
	}
	
	function onGuild(guild) {
		print("OnGuild");
		
		s_Servers.rawset(guild.ID, CDiscordGuild(guild));
	}
	
	function onGuildEdit(guild) {
		print("OnGuildEdit");
		
		s_Servers.rawset(guild.ID, CDiscordGuild(guild));
	}
	
	function onGuildDelete(guild) {
		print("OnGuildDelete");
		
		s_Servers.rawdelete(guild.ID);
	}
	
	function onChannel(channel) {
		print("onChannel");
		
		local serverID = channel.ServerID;
		if(serverID != null) {
			if(s_Servers.rawin(serverID)) {
				s_Servers[serverID].Channels.rawset(channel.ID, CDiscordChannel(channel));
			}
		}
		
		s_OtherChannels.rawset(channel.ID, CDiscordChannel(channel));
	}
	
	function onChannelEdit(channel) {
		print("onChannelEdit");
		
		local serverID = channel.ServerID;
		if(serverID != null) {
			if(s_Servers.rawin(serverID)) {
				s_Servers[serverID].Channels.rawset(channel.ID, CDiscordChannel(channel));
			}
		}
		
		s_OtherChannels.rawset(channel.ID, CDiscordChannel(channel));
	}
	
	function onChannelDelete(channel) {
		print("onChannelDelete");
		
		local serverID = channel.ServerID;
		if(serverID != null) {
			if(s_Servers.rawin(serverID)) {
				s_Servers[serverID].Channels.rawdelete(channel.ID);
			}
		}
		
		s_OtherChannels.rawdelete(channel.ID);
	}
	
	function onMember(serverID, member) {
		print("onMember");
		
		if(s_Servers.rawin(serverID)) {
			s_Servers[serverID].Members.rawset(member.ID, CDiscordMember(member));
		}
		
	}
	
	function onMemberEdit(serverID, user, roles, nick) {
		print("onMemberEdit");
		
		if(s_Servers.rawin(serverID)) {
			local server = s_Servers[serverID];
			
			if(server.Members.rawin(user.ID)) {
				local member = server.Members.rawget(user.ID);
				member.User = CDiscordUser(user);
				member.Roles = roles;
				member.Nick = nick;
			}
		}
	}
	
	function onMemberRemove(serverID, user) {
		print("onMemberRemove");
		
		if(s_Servers.rawin(serverID)) {
			s_Servers[serverID].Members.rawdelete(member.ID);
		}
	}
	
	function onRole(serverID, role) {
		print("onRole");
		
		if(s_Servers.rawin(serverID)) {
			s_Servers[serverID].Roles.rawset(role.ID, CDiscordRole(role));
		}
	}
	
	function onRoleEdit(serverID, role) {
		print("onRoleEdit");
		
		if(s_Servers.rawin(serverID)) {
			s_Servers[serverID].Roles.rawset(role.ID, CDiscordRole(role));
		}
	}
	
	function onRoleDelete(serverID, roleID) {
		print("onRoleDelete");
		
		if(s_Servers.rawin(serverID)) {
			s_Servers[serverID].Roles.rawdelete(roleID);
		}
	}
	
	function onUserEdit(user) {
		print("onUserEdit");
		
		foreach(server in s_Servers) {
			if(server.Members.rawin(user.ID)) {
				server.Members[user.ID].User = CDiscordUser(user);
				break;
			}
		}
		
		foreach(channel in s_OtherChannels) {
			if(channel.Recipients.rawin(user.ID)) {
				channel.Recipients[user.ID] = CDiscordUser(user);
				break;
			}
		}
	}
	
	function onError(code, message) {
		print(format("%d - %s", code, message));
	}
	
	function onDisconnect() {
		print("Discord session has disconnected.");
	}
	
	function onQuit() {
		print("Discord session has quit.")
	}
}

myDiscord <- MyDiscord();
myDiscord.Connect("token here");

function onDiscordUpdate(connID, eventType, ...) {
	if(sessions.rawin(connID)) {
		local session = sessions.rawget(connID);
		vargv.insert(0, session); //env
		session.eventFuncs[eventType].acall(vargv);
	}
}
Clone this wiki locally