simple discord python bot code example
Example 1: how to import discord in python
py -3 -m pip install -U discord.py
Example 2: how to make a discord bot python
import discord
class MyClient(discord.Client):
async def on_connect(self):
print('[LOGS] Connecting to discord!')
async def on_ready(self):
print('[LOGS] Bot is ready!')
print("""[LOGS] Logged in: {}\n[LOGS] ID: {}\n[LOGS] Number of users: {}""".format(self.bot.user.name, self.bot.user.id, len(set(self.bot.get_all_members()))))
await self.bot.change_presence(activity=discord.Game(name="Weeke is a god!"))
async def on_resumed(self):
print("\n[LOGS] Bot has resumed session!")
async def on_message(self, message):
if message.author == self.user:
return
if message.content == 'ping':
await ctx.send(f'Client Latency: {round(self.bot.latency * 1000)}')
client = MyClient()
client.run('token')
Example 3: Discord.py bot example
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='prefix here')
@bot.event
async def on_ready():
print('Logged in as: ' + bot.user.name)
print('Ready!\n')
@bot.command()
async def commandname(ctx, *, somevariable)
Code goes here
await ctx.send('Message')
bot.run('yourtoken')