Multi-Guild AFK System | Discord.JS V13 Series | #18

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to lease code we're going to be creating a multi-guild afk system as you can see on your screen if a member uses the slash afk set command and proceeds to set their afk status whenever that member is stacked the boat will automatically reply to the mentioner's message with an embed saying that that user that you just tagged has been afk for this time and for that status last reason and to return from being afk and removing the status you could use the afk return command or you can remove your afk status by simply sending a message in any channel you as a developer can choose what option to provide or you can provide both for this episode we're going to be using a database mongodb and an event and a command handler if you have all of these already set up then you can continue watching if you don't click the card up to the right of your screen to watch the tutorials on how to set them up let's get right into coding right so the first thing that we're going to do we're going to create a schema for our database we're going to go to our structures schemas and create a new file and call it whatever you want to call i'm going to call it fksystem.js and here we're going to require two properties from the mongos package package sorry we're gonna require a model and schema now we're gonna model dot export we're gonna export the model and we're gonna assign the name to be afk system for example or afk whatever you want to be and it's going to be a new schema and inside of the schema objects we can type guideline string user id string status string and time and make sure to remember the cap the letters and the small letters because it's very case sensitive now that that's done we're going to go to our commands to our systems we're gonna create our commands let's call it afk and here we gonna require also two properties from the discord package uh what we need is uh command interaction and message embed that's all we need and now here we can require so quantity required we're going to require the database that we just created so let's go to the file location schema and now we get a model command exports and the name of the recommend is gonna be afk i don't know what's up with me and typing today uh the description is going to be a multi guide system for example you can change this as much as you want and we can have options so we're going to create an array so the first option is going to be set which is going to be a sub command so type sub commands the description is going to say set your afk status and then we're gonna have options so your name is gonna be status and description is gonna be set your status for example whatever type it's gonna be a string and it's gonna be required so there's you can format this as much as you want but this is the way that i'm gonna do it this is just a blueprint and you can change whatever you want right for the other subcommand that we're going to be creating so let's copy paste this remove these options you will need them and change set to return and here you can say all right now here we're going to async and we're going to execute the interaction because that's all we need and here we can grab our parameter for that traction just like this and now what we're going to do here is we're going to grab a couple of objects from the attraction or properties so we can require actually type subtraction we're going to require files options user and created timestamp and if i'm going too fast you can pause the video as much as you want this is what we have done so far all right so now we're gonna create an embed a multi-use embed so we're gonna type equals to new message and let's set the author of it to be a user tag user sorry dynamic this is all the simple stuff that you probably probably already know so constantly afk status can we open it string you're gonna get it from the status option so we're grabbing the value of the status or wherever the user input was and here we're gonna open a try statement and if an error happens we're gonna catch the error function and we're going to console the slope better all right so we're going to tell the boat do whatever we say inside of this bracket and if it gives an error just console log it without crashing our boat now here we're going to create an if a switch statement so we're going to switch between the options don't get sub command because we have two sub commands so case one so in case the user selected the set sub command we're going to execute it execute whatever inside of this bracket no need for breaking now case return the others are command and we're going to execute whatever inside of this brackets all right for this one we're gonna um utilize the mongodb file with an update properties we have a weight dp dot find one and update so what this does so first of all we're gonna filter all right so the filter is we're going to tell that that we're going to tell the bot to go inside of the database and search for a document that has this gul id and this user id so guideline id user d equals to using the id so if it doesn't find one then it's going to create one and while we put it to find what an update one because we want to update the status if th the member use the command again so what we're going to do now is let's actually do this right do this let's remove the space put the comma here and here is going to be the updated one so the status is going to be if the status i wrote it wrong here just just like this all right and the time is going to be so time is going to be persons let's see and it's going to be created timestamp divided by 1000 so we can use the discord's new message timestamp and and here we're going to create a new object and the set and you can accept it through and what was it again up upset all right so um now that we're done we're gonna basically return with a message so let's actually change the color remember to be green and set the description to be your afk status has been updated soon and should be gonna provide the asp status all right and then beginner return interaction dot reply embeds we're gonna pass in the ambulance on the embassy array and then we're gonna set it to be affirmative so only the interaction user sees this message all right so now for the return option uh we're gonna await tv dot find um i'll actually delete one it's gonna be guide id so we're gonna filter the gallery to be this guy id and the user id it's going to be user.id all right and then we can copy this let's actually put it like this copy it as it's read and here your afk status has in very simple stuff so we're basically done with our command so this is the command it's pretty simple so now let's go to our um events create a new event inside of the message folder or wherever you want it to be let's call this for example afk system.js and here we can require also two properties from the discord package so we can type require discord and begin a required message and message embed that's all we need from the discord package in here beginner model or actually we're going to require the database here suppose dp we can see http equals and now we can model export and we can listen on that message create event and here we go async execute the message object and let's grab our parameters for the message object so select message all right so now in here we're going to check so if message with author is bought beginner return basically we're gonna ignore the message and now like i showed you in the beginning of the video you have two options to remove the status one option is to listen if the member sends a message and deletes the documents from the database let's do that real quick so wait dp looks fine actually delete one delete pretty simple stuff or actually message.author or tutor id so let's comment this out because i want my members to basically use the command instead but you can enable this too and now we're going to check so if message.mentions.number so if it has the size so if the message has mentions in it um remember mentions then we're going to execute this goal so we're going to create an embed so and now we're gonna go inside our database or actually begin a message both mentions dot members for each so for each member in dimensions we're gonna check it check the member user id inside of our database so we will have dp let's find one guys id it's going to be the same as a message and the user id is going to be the same as the member dot id and we're going to async error and data async error and we're gonna check if error so if an error occurs we can throw that error but if we find data we're going to return let's actually put it down here let's type embed.set description and here we're going to say um a dollar sign brackets member object went afk and here we're gonna type this bracket t actually and then at data dot time and then this two dots forever r and this bracket and and then we can accumulate a new line and we're going to say status and here we're going to provide that with the status data status and we're gonna return message dot reply and yeah message.supply embeds and then true what am i doing hey we're gonna just like this uh we can we make this actually affirmative now we can't all right so um yeah this is basically it's let's try our command now so let's go to the database and let me remove this fk system because i was testing some stuff fk systems all right so i deleted it if you go to leaks code you will see nothing about the fk system so now if i go here and type afk set hello youtube it should respond to us it didn't because the bot is not even online so let's turn it on alright now let's retry valid let's retry the command again so slash fk said hello youtube uh let's see we got an error error was pretty simple guys instead of array i put an object it shouldn't be obvious it should be an array so this is in our commands so let's remove this and type embed all right now let's restart the boat and now let's go to discord let's check the database if case let's remove this real quick all right so now afk sets low you can see here your afk status has been updated to hello and if i go to the database now refresh like you see here this is the guy id user id status and the time all right so now if i want to update so if i do the command again so afk sets hello again like you see it's not going to be creating a new document it's just gonna update this current one so like you see here hello again now if i try to tag myself links like the bot will automatically respond to me went to fp a few seconds ago and this is gonna automatically change so after three minutes gonna say say three minutes ago and stay to zero again all right so now if i type slash fk return it's going to say uafk status has been removed and if i go to the database like you see it's going to be removed so let's set again actually yeah let's do that whatever all right and now let's enable this where is afk system let's enable this and now if i try um i can see let's go to the database i'm in the database now if i send a message right if i go back to the database let's see it didn't oops didn't work because we have an error here guide guide is not defined because it should be message.kyle.id all right so let's retry again so i'm going to type a message i'm going to go to the database and like you see it's automatically deleted and the reason why we we went ahead and created for for for each function because sometimes less except whatever sometimes people mention a lot of people and the afk member might be one of them so if i take these two people nothing will happen now if i tag them again and tag myself because i'm afk it's gonna respond to me or it should respond to me am i even afk no i'm not so let's set all right so just go for this place it here why is um sorry guys it's because it's automatically deleting this so let's remove this quickly so i can test all right so let's set my fk again because i sent a message and it automatically removed me from the database so now if i try this like you see here lakes went to fk a few seconds ago so thank you guys for watching i hope to see you in the next episode thank you for all of our coffee supporters if you want to download all of these files all of this both port support main coffee linking down in the description alright see you guys later
Info
Channel: Lyxcode
Views: 796
Rating: undefined out of 5
Keywords: discord.js bot, discord, discord bot, discord AFK command, AFK command, AFK System, Global Command, AFK system discord.js, AFK command discord.js v13, sourcebin, afk command discord.js v13 monogdb, mongodb, how to create a discord bot
Id: fkWgzLqv2ko
Channel Id: undefined
Length: 16min 54sec (1014 seconds)
Published: Wed Dec 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.