|
| 1 | +# This code shows a very brief and small example of how to create a bot with our library. |
| 2 | +# This example does not cover all the features of the library, but it is enough to get you started. |
| 3 | +# In order to learn more about how to use the library, please head over to our documentation: |
| 4 | +# https://interactionspy.rtfd.io/en/latest/ |
| 5 | + |
| 6 | +# The first thing you need to do is import the library. |
| 7 | +import interactions |
| 8 | + |
| 9 | +# Now, let's create an instance of a bot. |
| 10 | +# When you make a bot, we refer to it as the "client." |
| 11 | +# The client is the main object that interacts with the Gateway, what talks to Discord. |
| 12 | +# The client is also the main object that interacts with the API, what makes requests with Discord. |
| 13 | +client = interactions.Client("your bot token will go here.") |
| 14 | + |
| 15 | +# With our client established, let's have the library inform us when the client is ready. |
| 16 | +# These are known as event listeners. An event listener can be established in one of two ways. |
| 17 | +# You can provide the name of the event, prefixed by an "on_", or by telling the event decorator what event it is. |
| 18 | +@client.event |
| 19 | +async def on_ready(): |
| 20 | + # We can use the client "me" attribute to get information about the bot. |
| 21 | + print(f"We're online! We've logged in as {client.me.name}.") |
| 22 | + |
| 23 | + # We're also able to use property methods to gather additional data. |
| 24 | + print(f"Our latency is {round(client.latency)} ms.") |
| 25 | + |
| 26 | + |
| 27 | +@client.event("message_create") |
| 28 | +async def name_this_however_you_want(message: interactions.Message): |
| 29 | + # Whenever we specify any other event type that isn't "READY," the function underneath |
| 30 | + # the decorator will most likely have an argument required. This argument is the data |
| 31 | + # that is being supplied back to us developers, which we call a data model. |
| 32 | + |
| 33 | + # In this example, we're listening to messages being created. This means we can expect |
| 34 | + # a "message" argument to be passed to the function, which will be the data model of such. |
| 35 | + |
| 36 | + # We can use the data model to access the data we need. |
| 37 | + print( |
| 38 | + f"We've received a message from {message.author.name}. The message is: {message.content}." |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +# Now, let's create a command. |
| 43 | +# A command is a function that is called when a user types out a command. |
| 44 | +# The command is called with a context object, which contains information about the user, the channel, and the guild. |
| 45 | +# Context is what we call the described information given from an interaction response, what comes from a command. |
| 46 | +# The context object in this case is a class for commands, but can also be one for components if used that way. |
| 47 | +@client.command(name="hello-world", description='A command that says "hello world!"') |
| 48 | +async def hello_world(ctx: interactions.CommandContext): |
| 49 | + # "ctx" is an abbreviation of the context object. |
| 50 | + # You don't need to type hint this, but it's recommended to do so. |
| 51 | + |
| 52 | + # Now, let's send back a response. |
| 53 | + # Note that when you make an interaction response, you can no longer run anything in this function. |
| 54 | + # The interaction response should be the LAST thing you do when a command is ran. |
| 55 | + await ctx.send("hello world!") |
| 56 | + |
| 57 | + # Because of this, this line of code right here will not execute. |
| 58 | + print("we ran.") |
| 59 | + |
| 60 | + |
| 61 | +# After we've declared all of the bot code we want, we need to tell the library to run our bot. |
| 62 | +# In this example, we've decided to do some things in a different way without explicitly saying it: |
| 63 | + |
| 64 | +# - we'll be syncing the commands automatically. |
| 65 | +# if you want to do this manually, you can do it by passing disable_sync=False in the Client |
| 66 | +# object on line 8. |
| 67 | +# - we are not setting a presence. |
| 68 | +# - we are not automatically sharding, and registering the connection under 1 shard. |
| 69 | +# - we are using default intents, which are Gateway intents excluding privileged ones. |
| 70 | +client.start() |
0 commit comments