Giter Club home page Giter Club logo

Comments (3)

Lyfhael avatar Lyfhael commented on June 7, 2024

I want to note that I've read the documentation available on github before making this post '-'
Also that if your explanation is a little big long, it's not a problem at all for me I really want to learn


arandomnewaccount:
hey sure, thanks for reading the docs btw I appreciate it. When running discum I recommend keeping log set to True since it prints out all the communications discum has with discord.
Discum only runs things if they're absolutely necessary.
So, the only thing that discum.Client does is create some objects and variables so you can start (https://docs.google.com/drawings/d/1_PSefOb5nlqEyEAQ14zPfFQuu8hUh9HVJHuV-ymSnTo/edit?usp=sharing).
That being said, since it's not needed to connect to discord's gateway on client start, discum doesn't automatically do this for you. Instead, you'll need to run bot.gateway.run(). The gateway is good for all real-time stuff. Note that the official discord docs are written for BOT accounts, so all that stuff about intents that they talk about does not apply to user accounts.

Now, discum's wraps are divided into 2 sections: http api (also called REST api) wraps and gateway api wraps. Anything that’s bot.FunctionName refers to an http api wrap (or related) and anything that’s bot.gateway.FunctionName refers to a gateway function.

resp is simply the response object (response from discord's gateway, packaged into an object). The reason it's an object is more for naming preferences (so you can do stuff like resp.event... checking and resp.parse... stuff). The raw response (format = dictionary, from discord) is placed in resp.raw. See here: https://github.com/Merubokkusu/Discord-S.C.U.M/blob/master/discum/gateway/response.py

Going over the fetchMembers example, here’s a line by line explanation:

#start up client, set variables, etc
import discum
bot = discum.Client(token='your token')

#create close_after_fetching function. Note, this takes in resp as the first input. All gateway commands need resp since they're all run after each response from discord's gateway
def close_after_fetching(resp, guild_id):
	if bot.gateway.finishedMemberFetching(guild_id):
		lenmembersfetched = len(bot.gateway.session.guild(guild_id).members) #this line is optional
		print(str(lenmembersfetched)+' members fetched') #this line is optional
		bot.gateway.removeCommand(close_after_fetching)
		bot.gateway.close()

#create get_members function. This doesn't take in resp as an input since all it does it piece together other functions
def get_members(guild_id, channel_id):
	bot.gateway.fetchMembers(guild_id, channel_id, keep="all", wait=1) #all this command does it add the fetchMembers command to the beginning of the command list (https://github.com/Merubokkusu/Discord-S.C.U.M/blob/master/discum/gateway/gateway.py#L307)
	bot.gateway.command({'function': close_after_fetching, 'params': {'guild_id': guild_id}}) #just adding the function to the command list
	bot.gateway.run() #now connecting to the gateway (all the commands we added to the command list will run after each response from discord. The bot.gateway.close_after_fetching() function will automatically close our connection when member fetching is finished)
	return bot.gateway.session.guild(guild_id).members #and finally, return the members

members = get_members('322850917248663552', '754536220826009670') #yes, the channel_id input is required

Above, to clarify what I said about bot.gateway.fetchMembers adding the "fetchMembers command to the beginning of the command list": the fetchMembers command is what I call a "combo" function, which means it sends data to discord and it receives and parses data. Here is the full fetchMembers command (the one that gets added to the command list): https://github.com/Merubokkusu/Discord-S.C.U.M/blob/master/discum/gateway/guild/combo.py#L82
Note that resp it the first param in that function (right after self).

So, yes, functions are announced before the gateway runs because the functions in the command list are run after each response from the gateway. Hope that helped.

from discord-s.c.u.m.

Lyfhael avatar Lyfhael commented on June 7, 2024

That helped a lot.
I have two final questions, When we add commands to the list then use gateway.run(), you say they run after each response, does that mean everytime Discord send me whatever response, it'll run all functions in the list ? So both close_after_fetching and get_members run everytime I get a response ?
And my second question, can I add commands to the list after I used gateway.run() or do I need to close() then .command() then .run() again


arandomnewaccount:

  1. The get_members function is not added to the command list. Instead, bot.gateway.fetchMembers adds the fetchMembers combo function to the beginning of the command list (and then the combo function runs every time you get a response). The close_after_fetching command does run after each response (in the example above). Any time you see a function that has resp as the first param, those functions are usually put in the command list.
  2. gateway.run() is blocking so you'll need to close first (if you're doing this all in the interpreter/command line). If you'd like to add functions to the command list while being connected to the gateway, you can (but you'll still need to declare the functions beforehand):
def somefunction(resp):
    print("another response received from discord") 

@bot.gateway.command
def helloworld(resp):
    if resp.event.message:
        m = resp.parsed.auto()
        if m["content"] == "wee":
            bot.gateway.command(somefunction)

bot.gateway.run()

You could use a similar implementation as the one above to fetch the member list whenever your client receives a certain message.
I suppose you could use threading instead to put functions into the command list while the gateway is running, but that's pretty messy and unnecessary in my opinion.

from discord-s.c.u.m.

Lyfhael avatar Lyfhael commented on June 7, 2024

Alright I think I have enough tools now to understand the rest by doing tests programs :D
You took some time to help me understand and I'm very grateful ^-^ Hope you have a good evening ! (I'll leave this issue open just a little bit in case I have a question coming back to my head, but I will close it in like 30 mins)


arandomnewaccount:
aight cool - glad it helped. Good evening to you too.
Happy coding!

from discord-s.c.u.m.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.