Upload files to "src"
This commit is contained in:
138
src/WrenVM.c
Normal file
138
src/WrenVM.c
Normal file
@@ -0,0 +1,138 @@
|
||||
#include "godsBot.h"
|
||||
|
||||
// creating the VM
|
||||
|
||||
static WrenVM* vm = NULL;
|
||||
|
||||
// writer
|
||||
static void writeFn(WrenVM* vm, const char* text){
|
||||
|
||||
(void)vm;
|
||||
printf("[Wren] %s\n", text);
|
||||
|
||||
}
|
||||
|
||||
// error handling
|
||||
static void errorFn(WrenVM* vm, WrenErrorType type, const char* module, int line, const char* msg){
|
||||
|
||||
(void)vm;
|
||||
|
||||
// displays different errors based on what it get
|
||||
switch (type){
|
||||
|
||||
case WREN_ERROR_COMPILE:
|
||||
fprintf(stderr,"[Error Compiling Wren] %s:%d: %s\n", module, line, msg);
|
||||
break;
|
||||
|
||||
case WREN_ERROR_RUNTIME:
|
||||
fprintf(stderr, "[Error In Runtime] %s\n", msg);
|
||||
break;
|
||||
|
||||
case WREN_ERROR_STACK_TRACE:
|
||||
fprintf(stderr, "[Wren Stack Trace] %s:%d: %s\n");
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// VM Init
|
||||
void WrenVmInit(void){
|
||||
if (vm) return;
|
||||
// configs
|
||||
WrenConfiguration config;
|
||||
wrenInitConfiguration(&config);
|
||||
config.writeFn = writeFn;
|
||||
config.errorFn = errorFn;
|
||||
|
||||
// new vm
|
||||
vm = wrenNewVM(&config);
|
||||
|
||||
// load scripts
|
||||
FILE* f = fopen("scripts/commands.wren", "r");
|
||||
|
||||
// checking if the file can be found
|
||||
if(!f){
|
||||
fprintf(stderr, "Faild to open command file\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// get file length
|
||||
fseek(f, 0, SEEK_END);
|
||||
long length = ftell(f);
|
||||
rewind(f);
|
||||
|
||||
// lock it in memory
|
||||
char* src = malloc(length + 1);
|
||||
fread(src, 1, length, f);
|
||||
src[length] = '\0';
|
||||
fclose(f);
|
||||
|
||||
// interpreter
|
||||
WrenInterpretResult result = wrenInterpret(vm, "commands", src);
|
||||
free(src);
|
||||
|
||||
// checking if the vm could load the scripts
|
||||
if(result != WREN_RESULT_SUCCESS){
|
||||
fprintf(stderr, "Fail to Load script(s)\n");
|
||||
}
|
||||
else{
|
||||
printf("loaded script(s)\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// handles the command file and passes it to wren
|
||||
char* WrenCommandHandler(const char* cmd, const char* args){
|
||||
|
||||
// checks if the vm has been set correctly if not exit out
|
||||
if(!vm){
|
||||
fprintf(stderr, "Vm is not Initialized\n");
|
||||
return strdup("Vm was not initialized");
|
||||
}
|
||||
|
||||
|
||||
// loads commands
|
||||
wrenEnsureSlots(vm, 1);
|
||||
wrenGetVariable(vm, "commands", "Commands", 0);
|
||||
WrenHandle* classHandle = wrenGetSlotHandle(vm, 0);
|
||||
|
||||
// interprets the command
|
||||
char sig[64];
|
||||
snprintf(sig, sizeof(sig), "%s()", cmd);
|
||||
WrenHandle* methodHandle = wrenMakeCallHandle(vm, sig);
|
||||
|
||||
// Set Wren slot to 1
|
||||
wrenEnsureSlots(vm, 1);
|
||||
wrenSetSlotHandle(vm, 0, classHandle);
|
||||
|
||||
// interpets resluts of input?
|
||||
WrenInterpretResult result = wrenCall(vm, methodHandle);
|
||||
|
||||
if(result != WREN_RESULT_SUCCESS){
|
||||
|
||||
fprintf(stderr, "Wren: rror running method");
|
||||
wrenReleaseHandle(vm, classHandle);
|
||||
wrenReleaseHandle(vm, methodHandle);
|
||||
return strdup("Wren command error");
|
||||
}
|
||||
|
||||
const char* out = wrenGetSlotString(vm, 0);
|
||||
char* reply = strdup(out ? out: "No return");
|
||||
|
||||
wrenReleaseHandle(vm, classHandle);
|
||||
wrenReleaseHandle(vm, methodHandle);
|
||||
|
||||
return reply;
|
||||
}
|
||||
|
||||
|
||||
// releses the memory of the vm
|
||||
void WrenVmFree(void){
|
||||
|
||||
if(vm){
|
||||
wrenFreeVM(vm);
|
||||
vm = NULL;
|
||||
}
|
||||
|
||||
}
|
||||
64
src/discordBot.c
Normal file
64
src/discordBot.c
Normal file
@@ -0,0 +1,64 @@
|
||||
#include "godsBot.h"
|
||||
|
||||
|
||||
// one the bot logs in it runs this first
|
||||
static void on_ready(struct discord *client, const struct discord_ready *event){
|
||||
|
||||
printf("bot logged in as: %s#%s\n", event->user->username, event->user->discriminator);
|
||||
|
||||
}
|
||||
|
||||
// handle messages
|
||||
void on_message(struct discord *client, const struct discord_message *msg){
|
||||
|
||||
// check if message should go through
|
||||
if (!parseMessage(msg)) return;
|
||||
|
||||
// split the messge into commands and aguments
|
||||
|
||||
// get command
|
||||
char *content = strdup(msg->content +1);
|
||||
char *space = strchr(content, ' ');
|
||||
char *cmd = content;
|
||||
|
||||
// get args
|
||||
char *args = "";
|
||||
|
||||
if(space){
|
||||
*space = '\0';
|
||||
args = space +1;
|
||||
}
|
||||
|
||||
// send and recive from wren
|
||||
char *response = WrenCommandHandler(cmd, args);
|
||||
struct discord_create_message params = { .content = response };
|
||||
discord_create_message(client, msg->channel_id, ¶ms, NULL);
|
||||
|
||||
// manage memory
|
||||
free(content);
|
||||
free(response);
|
||||
|
||||
}
|
||||
|
||||
// run da bot
|
||||
void discordBotRunner(void){
|
||||
|
||||
// load bot config from config.json and set intents
|
||||
struct discord *client = discord_config_init("config.json");
|
||||
discord_set_on_ready(client, &on_ready);
|
||||
discord_set_on_message_create(client, &on_message);
|
||||
|
||||
discord_add_intents(
|
||||
client,
|
||||
DISCORD_GATEWAY_MESSAGE_CONTENT |
|
||||
DISCORD_GATEWAY_GUILD_MESSAGES |
|
||||
DISCORD_GATEWAY_DIRECT_MESSAGES
|
||||
);
|
||||
|
||||
printf("starting bot\n");
|
||||
|
||||
// clean up
|
||||
discord_run(client);
|
||||
discord_cleanup(client);
|
||||
|
||||
}
|
||||
11
src/main.c
Normal file
11
src/main.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "godsBot.h"
|
||||
|
||||
// runner ig
|
||||
int main(void){
|
||||
|
||||
WrenVmInit();
|
||||
discordBotRunner();
|
||||
WrenVmFree();
|
||||
|
||||
return 0;
|
||||
}
|
||||
12
src/messageParser.c
Normal file
12
src/messageParser.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "godsBot.h"
|
||||
|
||||
bool parseMessage(const struct discord_message *msg){
|
||||
|
||||
if(!msg) return false;
|
||||
if(!msg->author) return false;
|
||||
if(msg->author->bot) return false;
|
||||
if(!msg->content) return false;
|
||||
if(msg->content[0] != '$') return false;
|
||||
return true;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user