1from discord.ext import commands
2
3class Test_Cog(commands.Cog):
4 def __init__(self, bot):
5 self.bot = bot # defining bot as global var in class
6
7 @commands.Cog.listener() # this is a decorator for events/listeners
8 async def on_ready(self):
9 print('Bot is ready!.')
10
11 @commands.command() # this is for making a command
12 async def ping(self, ctx):
13 await ctx.send(f'Pong! {round(self.bot.latency * 1000)}')
14
15def setup(bot): # a extension must have a setup function
16 bot.add_cog(Test_Cog(bot)) # adding a cog
1# bot file
2import os
3from discord.ext import commands
4
5bot = commands.Bot(command_prefix='.')
6# I am assuming that you have a test.py cog in a cogs folder
7bot.load_extension('cogs.test') # this is good but you can make it better
8
9for filename in os.listdir('./cogs'):
10 if filename.endswith('.py'):
11 bot.load_extension(f'cogs.{filename[:-3]}')
12
13 else:
14 print(f'Unable to load {filename[:-3]}')
15
16bot.run(token)
1import discord
2
3client = discord.Client()
4
5@client.event
6async def on_ready():
7 print('Logged in as {0.user}'.format(client))
8
9@client.event
10async def on_message(message):
11 if message.author == client.user:
12 return
13
14 if message.content.startswith('$hello'):
15 await message.channel.send('Hello!')
16
17client.run('your token here')
1# Discord.py is a API wrapper for python.
2Docs = "https://discordpy.readthedocs.io/en/latest/"
3PyPI = "pip install -U discord.py"
4
5# --- A simple bot ---
6
7import discord
8from discord.ext import commands
9
10client = commands.Bot(comand_prefix='bot prefix here') # You can choose your own prefix here
11
12@client.event()
13async def on_ready(): # When the bot starts
14 print(f"Bot online and logged in as {client.user}")
15
16# A simple command
17@client.command(aliases=["ms", "aliases!"]) # You make make the command respond to other commands too
18async def ping(ctx, a_variable): # a_variable is a parameter you use in the command
19 await ctx.send(f"Pong! {round(client.latency * 1000)}ms. Your input was {a_variable}")
20
21client.run('your token here') # Running the bot
1import discord
2
3client = discord.Client()
4
5@client.event
6async def on_ready():
7 print('We have logged in as {0.user}'.format(client))
8
9@client.event
10async def on_message(message):
11 if message.author == client.user:
12 return
13
14 if message.content.startswith('$hello'):
15 await message.channel.send('Hello!')
16
17client.run('your token here')
1import discord
2
3class MyClient(discord.Client):
4
5 async def on_ready(self):
6 print('Logged on as', self.user)
7
8 async def on_message(self, message):
9 word_list = ['cheat', 'cheats', 'hack', 'hacks', 'internal', 'external', 'ddos', 'denial of service']
10
11 # don't respond to ourselves
12 if message.author == self.user:
13 return
14
15 messageContent = message.content
16 if len(messageContent) > 0:
17 for word in word_list:
18 if word in messageContent:
19 await message.delete()
20 await message.channel.send('Do not say that!')
21
22 messageattachments = message.attachments
23 if len(messageattachments) > 0:
24 for attachment in messageattachments:
25 if attachment.filename.endswith(".dll"):
26 await message.delete()
27 await message.channel.send("No DLL's allowed!")
28 elif attachment.filename.endswith('.exe'):
29 await message.delete()
30 await message.channel.send("No EXE's allowed!")
31 else:
32 break
33
34client = MyClient()
35client.run('OTEwNTg2MDQzMzE5ODc3NjMy.YZU_Vg.kdr4g8pqGC8Z2osYy4Aj3uLfLXE')