1import discord
2from discord.ext import commands
3
4bot = commands.Bot(command_prefix="!", description="The description")
5
6@bot.event
7async def on_ready():
8 print("Ready !")
9
10@bot.command()
11async def ping(ctx):
12 await ctx.send('**pong**')
13
14bot.run("enter the token here between the quotes")
1# pip install discord
2
3import discord
4
5class MyClient(discord.Client):
6 async def on_connect(self):
7 print('[LOGS] Connecting to discord!')
8
9 async def on_ready(self):
10 print('[LOGS] Bot is ready!')
11 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()))))
12 await self.bot.change_presence(activity=discord.Game(name="Weeke is a god!"))
13
14 async def on_resumed(self):
15 print("\n[LOGS] Bot has resumed session!")
16
17 async def on_message(self, message):
18 # don't respond to ourselves
19 if message.author == self.user:
20 return
21
22 if message.content == 'ping':
23 await ctx.send(f'Client Latency: {round(self.bot.latency * 1000)}')
24
25client = MyClient()
26client.run('token')
1#________________________________________________________________________________________#
2# #
3# How to make a discord bot #
4#________________________________________________________________________________________#
5
6
7# BE AWARE! YOU NEED TO CUSTOMIZE/CONFIGURE THIS CODE OTHERWISE IT WON'T WORK.
8# THIS CODE IS JUST THE BASICS
9
10
11
12# IMPORT DISCORD.PY. ALLOWS ACCESS TO DISCORD'S API.
13import discord
14
15# IMPORTS EXTENSIONS FOR COMMANDS
16from discord.ext import commands
17
18
19# IMPORT THE OS MODULE.
20import os
21
22# IMPORT LOAD_DOTENV FUNCTION FROM DOTENV MODULE.
23from dotenv import load_dotenv
24
25# IMPORT LOGGING
26
27import logging
28
29
30# LOADS THE .ENV FILE THAT RESIDES ON THE SAME LEVEL AS THE SCRIPT.
31load_dotenv()
32
33# GRAB THE API TOKEN FROM THE .ENV FILE.
34DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
35
36# GETS THE CLIENT OBJECT FROM DISCORD.PY. CLIENT IS SYNONYMOUS WITH BOT.
37bot = discord.Client()
38
39logger = logging.getLogger('discord')
40logger.setLevel(logging.DEBUG)
41handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
42handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
43logger.addHandler(handler)
44
45
46bot = commands.Bot(command_prefix='Yourprefix')
47
48# UNDER THIS LINE OF CODE ARE THE COMMANDS FOR THE BOT. YOU CAN ADD/CHANGE THOSE SAFELY WITHOUT DESTORYING THE CODE
49
50@bot.command()
51async def test(ctx, *, arg):
52 await ctx.send(arg, file=discord.File('yourfile.png'))
53
54
55@bot.command()
56async def laugh(ctx):
57 await ctx.send("typeherealink")
58
59
60
61
62@bot.command()
63async def die(ctx):
64 exit()
65
66
67
68# EXECUTES THE BOT WITH THE SPECIFIED TOKEN. DON'T REMOVE THIS LINE OF CODE JUST CHANGE THE "DISCORD_TOKEN" PART TO YOUR DISCORD BOT TOKEN
69bot.run(DISCORD_TOKEN)
70
71#________________________________________________________________________________________#
72
73# If this code helped you please leave a like on it. If you want to see more of this follow me
74# Or just take a look at another answer of my answers
75#
76# THIS CODE HAS BEEN MADE BY : Vast Vicuña
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('token here')
1import discord
2from discord.ext import commands
3
4client = commands.Bot(command_prefix=".")
5
6@client.event
7async def on_ready():
8 print("Ready!")
9
10bot.run("TOKEN")
1import discord
2from discord.ext import commands
3
4client = commands.Bot(comand_prefix='bot prefix here') # You can choose your own prefix here
5
6@client.event()
7async def on_ready(): # When the bot starts
8 print(f"Bot online and logged in as {client.user}")
9
10# A simple command
11@client.command(aliases=["ms", "aliases!"]) # You make make the command respond to other commands too
12async def ping(ctx, a_variable): # a_variable is a argument you use in the command
13 await ctx.send(f"Pong! {round(client.latency * 1000)}ms. Your input was {a_variable}")
14
15client.run('your token here') # Running the bot