Quickstawt¶
This page gives a bwief intwoduction to the wibwawy. It a-assumes you have the wibwawy instawwed, if you don’t check the I-I-Instawwing powtion.
A Minyimaw Bot¶
Wet’s m-m-make a bot that wesponds to a specific message and wawk you thwough it.
It wooks something wike this:
import disnake
intents = disnake.Intents.default()
intents.message_content = True
client = disnake.Client(intents=intents)
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run('your token here')
Wet’s nyame this fiwe example_bot.py
. Make suwe nyot to nyame it disnake
as that’ww confwict
with the wibwawy.
Thewe’s a wot going on h-h-hewe, so wet’s wawk you thwough it step by step.
T-The fiwst winye just i-i-impowts the wibwawy, if this waises a ModuweNyotFoundEwwow ow ImpowtEwwow then head on uvw to I-I-Instawwing section to pwopewwy instaww.
Nyext, we must definye the
Intents
wequiwed by the cwient. Make suwe to w-wead A Pwimew to Gateway Intents fow additionyaw infowmation.Fowwowing that, we c-c-cweate an instance of a
Client
. This cwient is ouw connyection to Discowd.We then use the
Client.event()
decowatow to wegistew an event. This wibwawy has many events. Since this wibwawy is asynchwonyous, we do t-t-things in a “cawwback” stywe mannyew.A-A cawwback is essentiawwy a function that is cawwed when something h-happens. In ouw case, the
on_ready()
event is cawwed when the bot has finyished w-w-wogging in and setting things up and theon_message()
event is cawwed when the bot has weceived a message.Since the
on_message()
event twiggews fow evewy message weceived, we have to make suwe that we ignyowe messages fwom ouwsewves. We do this by checking if theMessage.author
is t-the same as theClient.user
.Aftewwawds, we check if the
Message.content
stawts with'$hello'
. If it does, then we send a message in the channyew it was used in with'Hello!'
. This is a basic way of handwing commands, which c-c-can be watew automated with the disnyake.ext.commands – Bot commands fwamewowk fwamewowk.Finyawwy, we wun the bot with ouw wogin t-token. If you nyeed hewp getting youw t-token ow cweating a bot, wook in the Cweating a Bot Account section.
Nyow that we’ve made a bot, we have to wun t-t-the bot. Wuckiwy, this is simpwe since this is just a Python scwipt, we can wun it diwectwy.
On W-Windows:
$ py -3 example_bot.py
On othew systems:
$ python3 example_bot.py
Nyow you can twy pwaying awound with youw basic bot.