Skip to content

Commit 5bbf0ab

Browse files
committed
style: add examples
1 parent 7a2ed8f commit 5bbf0ab

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

examples/bot.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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()

interactions/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
"""
2-
(interactions)
3-
discord-interactions
2+
interactions.py
43
5-
Easy, simple, scalable and modular: a Python API wrapper for interactions.
4+
Easy, simple, scalable and modular: a Python library for interactions.
65
76
To see the documentation, please head over to the link here:
8-
https://discord-interactions.rtfd.io/en/latest for ``stable`` builds.
9-
https://discord-interactions.rtfd.io/en/unstable for ``unstable`` builds.
7+
https://interactionspy.rtfd.io/en/latest for ``stable`` builds.
8+
https://interactionspy.rtfd.io/en/unstable for ``unstable`` builds.
109
11-
(c) 2021 goverfl0w.
12-
Co-authored by DeltaXW.
10+
(c) 2021 interactions-py.
1311
"""
1412
from .api.models.channel import * # noqa: F401 F403
1513
from .api.models.flags import * # noqa: F401 F403

0 commit comments

Comments
 (0)