-
Notifications
You must be signed in to change notification settings - Fork 36
Home
OllysCoding edited this page Oct 15, 2016
·
3 revisions
DiscordUnity feeds back in two ways, callbacks and events.
When trying to do something within DiscordUnity, you will always povide a callback method. This means, when the operation is completed (i.e sending a message), on the next frame that callback will be called.
client.CreateServer("Server Name", "london", null, ServerCreatedCallback)
Events are for operations which are not initiated by you (i.e receiving a message), and work like they would for any other C# discord library.
client.OnMessageCreated += MessageCreatedEvent
//Declare Our Client
private DiscordClient _client;
//Method Called On Start
void Start()
{
_client = new DiscordClient();
Debug.Log("Connecting to client");
//Connect to the client as a bot account, make sure to include a callback
_client.StartBot("Bot YourBotToken", ClientConnected);
}
//This method will be called when our client connects
void ClientConnected(DiscordClient client, string result, DiscordError error)
{
//Check to make sure we didn't fail to connect
if (error.failed)
{
Debug.Log("Error Connecting to bot account: " + error.message);
return;
}
//If we haven't failed, lets add an event for when we receive a message
client.OnMessageCreated += MessageCreatedEvent;
}
//This will be called when our client receives a message
private void MessageCreatedEvent(object sender, DiscordMessageArgs e)
{
Debug.Log("Message Received: " + e.message.content);
}
//We need to make sure we update the client every frame, so that we can receive our events and callbacks.
void Update()
{
if (_client != null)
{
if (_client.isUpdatable)
{
_client.Update();
}
}
}
##Help: If you are still unsure on how DiscordUnity works, make sure you pop into the DiscordUnity channel on discord, and we will be happy to help: https://discord.gg/TP7syAT