Skip to content

Instantly share code, notes, and snippets.

@jerryan999
Last active March 29, 2022 15:48
Show Gist options
  • Save jerryan999/fc7bd92125317297696e3ebe3c2b697d to your computer and use it in GitHub Desktop.
Save jerryan999/fc7bd92125317297696e3ebe3c2b697d to your computer and use it in GitHub Desktop.
discord bot example
import discord
import asyncio
class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
print('------')
self.bg_task = self.loop.create_task(self.my_background_task())
async def my_background_task(self):
await self.wait_until_ready()
counter = 0
channel = self.get_channel(958388246751105094) # your channel ID goes here
while not self.is_closed():
counter += 1
await channel.send(counter)
await asyncio.sleep(10) # task runs every 10 seconds
client = MyClient()
client.run('YOUR_TOKEN')
# encoding: utf-8
from discord.ext import commands
bot = commands.Bot(command_prefix='$')
@bot.event
async def on_ready():
print('Discord Client Logged on as', bot.user)
@bot.command()
async def ping(ctx):
await ctx.send('pong')
bot.run('YOUR_TOKEN')
# encoding: utf-8
import discord
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
async def on_message(self, message):
# don't respond to ourselves
if message.author == self.user:
return
if message.content == 'ping':
await message.channel.send('pong')
client = MyClient()
client.run('YOUR_TOKEN')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment