what's going on everybody welcome to
part three of the making BOTS with
discord pie tutorial series in this
video what we're going to be working on
is something a little different so most
of this stuff is just using you know a
command and have that command do some
stuff so you're just like reading a
message and performing some logic it's
all basically the same stuff it's just
when you know how creative do you want
to get with it
now what I want to do in this video is
talk about how we can time certain
actions so that's not really something
we've covered yet so let's go ahead and
get into it so the thing that I want a
time is I want to just output to like a
CSV these like user metrics basically
but I don't want to do it I don't want
to like have to sit here and call the
command every time I want it to happen
I'd like it just to automatically happen
so the first thing that I want to do is
put this into a function because I
basically want the exact same thing to
occur I just want it to occur in its own
function the other thing we really need
to do is like Centex guild because we're
gonna do this like right now we could
pass the guild to the function so like
for example I'll just come up here and
we'll just define community report and
then community report will get guild
passed to it and then we're just going
to take this copy paste and have that
over and then we can just return online
Idol offline then we do this and in fact
actually I think I'll do this copy that
and then we come in here and we'll say
online Idol offline equals community
report and then the sent X underscore
guild so now that should give us the
community report and in fact let's just
go ahead and test that real quick make
sure I didn't make any mistakes
Senta bots rapport oh we didn't neck
oscy no we do so how come he didn't um
community report sent they bought
community did i spell it wrong now sent
it bata oh okay I forgot to died I did
an underscore rather than a dot okay so
we did get an error sent X guild is not
defined do we yeah okay so I needed this
needs to be changed to guild save come
back over here let me sent a bot log out
and let's try that one more time close
out of these two okay so this time let's
run the community report make sure
that's working as intended okay great
alright so we've moved that we just move
this up here so now we want to do is add
a background task that also calls this
but saves it so the way that we're gonna
do that is by first first I think what
we'll do is we'll just write the
background task so just somewhere up
here actually maybe after the community
report we will write the task so this
will be a sync deaf user metrics
background and background task empty
parms here and then what we want to do
is a wait client dot wait until ready
and then we need that syntax guild so
part of me kind of regrets going with
the rapper's because if this was oo P
this would be so simple you would give
it an attribute and in this case there's
there are ways that we can share this
like basically I just want to keep
defining it it's not the end of the
world like we could have like a maybe a
constant so centex guild equals
client I get guilt and then the constant
we could do that or we can use Global's
so I'm gonna use a global I know that's
gonna just work some people you could
use like a a tuple or like a dictionary
or something like that maybe but I'm
gonna just I'm just going to go ahead
and global this so I'm gonna global
Global Centex guild and then we after we
do that we can just come up to this wait
you know a wait client until ready and
then once it's ready we'll global the
Centex killed so then while not client
dot is closed this is just a method that
we can call on the client so wow that's
not the case we're gonna throw in a try
except because if we don't do this at
any point if it fails in this loop or
background task is gonna quit and it's
not gonna continue running so we are
going to try and accept exception as e
and we'll just print string e there and
what we want to try is basically this
exact what is this this this line here
so I'm just going to copy it come back
up here paste on line Idol offline
community report semtex guild
we've global dit so we should be all set
to use it there then what we want to do
now that we have those metrics is with
open and then we'll just call this user
metrics dot CSV we're gonna open with
the intention to append as F we're gonna
F dot right we're gonna write an F
string that has four values actually
three for the first value will be time
so I'm gonna go ahead and actually I
think we're not using sis anymore that
didn't solve it for us so I'm gonna get
rid of sis that's where usually if you
don't use something though it says hey
you're not using it are we actually
using it somewhere we're not that's kind
of weird
usually it alerts me oh maybe it is
alerting me usually has a little dot
though anyway let's import time we're
also going to import
a sink i/o and we're gonna use we're
importing a sink a sink i/o so we can do
an asynchronous sleep as well this way
doesn't impact the rest of our
functionality which by the way if you
don't know much about co-routines and
all that kind of stuff since we're using
them so heavily here if you go to Python
programming net and search for a sink
you'll find a sink i/o basics go ahead
and click on that and that'll take you
here and you can see you can get a
tutorial on a sink i/o so yeah okay so
back to here so what we want to write is
will do the int value of time dot time
we will do then we'll just do on line
idle off line and then we'll do a new
line and that should be good so once
we've done that once we've saved that
file we're just gonna do in a wait async
i/o dot sleep for how long now I'm
probably gonna do something like 600 I
don't really need these metrics like
super frequently but for the purpose of
the tutorial I'm gonna say 5 4 now also
if we do hit an error let's go ahead and
sleep there as well no reason to hit
that error like a million times if we
hit an error probably it'll be something
wrong with the guild it will be my guess
it's gonna take a second to pull the
Gildan even though which is kind of
weird because I would think until ready
but maybe if we could wait until oh the
guild isn't let's see let's define the
guild let's cut that here paste here
that was stupid so it wouldn't work
until we had a message yeah cut here
paste here and then let's global sent X
killed here because we're gonna need it
and I bet that'll solve the issue for us
we'll see if it doesn't though so let's
just go ahead and save that now
what we need to do is call this to
actually occur so this is just a
background task that we want to run so
where should we call it well we we could
call it in a few places but I'm gonna
call it right before client dot run
that's the way they suggest we do it so
that's the way I'm gonna do it
so client dot loop dot create underscore
task and then we just want to create
that task name so or basically run it so
it's that user metrics background task
don't forget your parentheses boom and
that should be everything let's go ahead
and run it let's see what happens see if
we hit any errors I see sent it by in
the chat set Dex guild not defined
initially let's just wait it's actually
kind of weird we got some chat
information and then it said we logged
in that's kind of weird I'm not sure
what to think about that I don't know if
it's because like Luke create tasks but
Luke dot create tasks starts with wait
until ready if anything maybe we should
define the guild in here in global it
everywhere else but then it would be
defined every no because it's in the
wild not I want to try that let's see if
that will cause problem but we'll do
that in a moment let me pull up here so
here's user metrics dot CSV sure enough
there we have all of our metrics
hopefully every five seconds any second
now there it is there's an update so
pretty cool
looks like we've 88 89 90 we're gaining
users and then we lost a couple darn it
interesting so over time I forgot
already this is probably online idle and
then offline and then you could add
these all up to get total users yeah and
then this is just time the UNIX
timestamp cool
okay so last thing I want to try is
rather than on ready let's continue
globe Ling semtex kill bullets cut it
here
and let's paste centex guild in this
background task just curious so I'm
gonna kick out the bots real quick oh
okay I just took a second he didn't like
leave okay and then I'm gonna rerun it
let's see how we do he is back no errors
yet cool okay so we'll stuff it there
global probably isn't the best way to
handle this problem but at least we're
not hitting an error anymore I just
really wanted to solve that okay so I
think that's enough for now
in the next tutorial what I'd like to
cover is so I'm gonna I would like to
let this run let's fix this I would like
to let this run for at least like a day
or two days or something like that and
then show you guys like the next command
would be to like change community or
report to actually be like a cool report
rather than just numbers what we could
do is have have them use like matplotlib
or pi gal or any of the million plotting
options that we have available to us to
generate an output to like a PNG image
and then we can actually submit that PNG
image with the bots so it can actually
embed that image and upload that image
so when we do a community report we can
see like trends over time and stuff so
that would be pretty darn awesome so
that's what I'd like to do next because
again most of this stuff is just kind of
commands stuff like that it's just a
bunch of the same stuff so I think
that's kind of boring but I'd like to do
is something a little different so
attaching files and uploading files I
don't yet know really how to do that and
that's something a little unique so
that's what we're gonna be doing in the
next tutorial quick shout out to my most
recent members Tosh Oleg awanee
Scott Harwood our June sock diva I don't
know
Stefan our and some guy thank you guys
very much for your support you guys are
awesome
especially Scott Harwood okay so that's
it for now questions comments concerns
if you have a suggestion about how to
handle and not use Global's in a nice
clean way let me know in the comments
below otherwise I will see you guys in
the next tutorial where we are uploading
some beautiful charts and if my user
metrics go down it's going to take a few
more days because I can only show in the
increasing trend till next time