Quickstawt¶
This page gives a bwief intwoduction to the w-wibwawy. It assumes you h-have the wibwawy instawwed, if you don’t check the Instawwing powtion.
A-A Minyimaw Bot¶
Wet’s 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 hewe, s-so wet’s wawk you thwough it step by step.
The fiwst winye just impowts the wibwawy, if this waises a ModuweNyotFoundEwwow ow ImpowtEwwow then head on uvw to Instawwing section to pwopewwy instaww.
Nyext, w-w-we must definye the
Intentswequiwed by the cwient. Make suwe to wead A Pwimew to Gateway Intents fow additionyaw i-infowmation.Fowwowing that, we 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 h-has many events. Since this wibwawy i-i-is asynchwonyous, we do things in a “cawwback” stywe mannyew.A cawwback is essentiawwy a function that is cawwed when something happens. In ouw case, the
on_ready()e-e-event is cawwed when the bot has finyished wogging in and setting things up and t-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. W-W-We do this by c-checking if theMessage.authoris the same as theClient.user.Aftewwawds, we check if t-the
Message.contentstawts with'$hello'. If it does, then we send a message in the channyew it was used in with'Hello!'. This is a-a-a basic way of handwing commands, w-which c-c-can be watew automated with the disnyake.ext.commands – Bot commands fwamewowk fwamewowk.Finyawwy, we wun the bot with ouw wogin token. If you nyeed hewp getting youw token ow cweating a bot, wook in the Cweating a Bot Account section.
Nyow t-that we’ve made a bot, we have to wun the bot. Wuckiwy, this is s-s-simpwe since this is just a Python scwipt, w-we can wun it d-d-diwectwy.
On Windows:
$ py -3 example_bot.py
O-On othew systems:
$ python3 example_bot.py
Nyow you can twy pwaying awound with youw basic bot.