I'm trying to set up a bot in Python that is able to post in a Discord channel when the script is executed. I have set up the bot already and added it to the server but my Python script is raising errors and I'm not able to solve it.
I've already set up a different bot but that leads to the same result. Can someone help?
I'm using Jupyter Notebook with this code:
## BOT
# IMPORT DISCORD.PY. ALLOWS ACCESS TO DISCORD'S API.
import discord
# GETS THE CLIENT OBJECT FROM DISCORD.PY. CLIENT IS SYNONYMOUS WITH BOT.
bot = discord.Client(intents=discord.Intents.default())
# EVENT LISTENER FOR WHEN THE BOT HAS SWITCHED FROM OFFLINE TO ONLINE.
@bot.event
async def on_ready():
# CREATES A COUNTER TO KEEP TRACK OF HOW MANY GUILDS / SERVERS THE BOT IS CONNECTED TO.
guild_count = 0
# LOOPS THROUGH ALL THE GUILD / SERVERS THAT THE BOT IS ASSOCIATED WITH.
for guild in bot.guilds:
# PRINT THE SERVER'S ID AND NAME.
print(f"- {guild.id} (name: {guild.name})")
# INCREMENTS THE GUILD COUNTER.
guild_count = guild_count + 1
# PRINTS HOW MANY GUILDS / SERVERS THE BOT IS IN.
print("SampleDiscordBot is in " + str(guild_count) + " guilds.")
# EXECUTES THE BOT WITH THE SPECIFIED TOKEN
bot.run('token')
Error
Eventually I want to have full control of the bot. So I can enter in Python say 'Hello' and it posts 'Hello' to the channel etc.
You should put your code inside a file (for example, let's call it bot.py
) and run the code via an IDE or the command line.
To run via the command line, go to the folder where your script is and execute py3 bot.py
if you're on windows, or python3 bot.py
if you're on macOS or Linux.