1// Import the discord.js module
2const Discord = require('discord.js');
3
4// Create an instance of a Discord client
5const client = new Discord.Client();
6
7/**
8 * The ready event is vital, it means that only _after_ this will your bot start reacting to information
9 * received from Discord
10 */
11client.on('ready', () => {
12 console.log('I am ready!');
13});
14
15client.on('message', message => {
16 // Ignore messages that aren't from a guild
17 if (!message.guild) return;
18
19 // if the message content starts with "!ban"
20 if (message.content.startsWith('!ban')) {
21 // Assuming we mention someone in the message, this will return the user
22 // Read more about mentions over at https://discord.js.org/#/docs/main/master/class/MessageMentions
23 const user = message.mentions.users.first();
24 // If we have a user mentioned
25 if (user) {
26 // Now we get the member from the user
27 const member = message.guild.members.resolve(user);
28 // If the member is in the guild
29 if (member) {
30 /**
31 * Ban the member
32 * Make sure you run this on a member, not a user!
33 * There are big differences between a user and a member
34 * Read more about what ban options there are over at
35 * https://discord.js.org/#/docs/main/master/class/GuildMember?scrollTo=ban
36 */
37 member
38 .ban({
39 reason: 'They were bad!',
40 })
41 .then(() => {
42 // We let the message author know we were able to ban the person
43 message.channel.send(`Successfully banned ${user.tag}`);
44 })
45 .catch(err => {
46 // An error happened
47 // This is generally due to the bot not being able to ban the member,
48 // either due to missing permissions or role hierarchy
49 message.channel.send('I was unable to ban the member');
50 // Log the error
51 console.error(err);
52 });
53 } else {
54 // The mentioned user isn't in this guild
55 message.channel.send("That user isn't in this guild!");
56 }
57 } else {
58 // Otherwise, if no user was mentioned
59 message.channel.send("You didn't mention the user to ban!");
60 }
61 }
62});
63
64// Log our bot in using the token from https://discord.com/developers/applications
65client.login('your token here');
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')
1
2#Import essentials
3import discord
4from discord.ext import commands
5import asyncio
6#Some things that makes your life easier! (aliases to make code shorter)
7client = commands.Bot(command_prefix='!') #change it if you want
8token = 'YOUR TOKEN HERE' #Put your token here
9#Making a first text command! (Respond's when a user triggers it on Discord)
10@client.command()
11async def hello(ctx):
12 await ctx.send('Hello I am a Test Bot!')
13#Tell's us if the bot is running / Runs the bot on Discord
14@client.event
15async def on_ready():
16 print('Hello, I am now running')
17
18
19
20client.run(token)
1const Discord = require('discord.js');
2const client = new Discord.Client();
3
4client.on('ready', () => {
5 console.log(`Logged in as ${client.user.tag}!`);
6});
7
8client.on('interactionCreate', async interaction => {
9 if (!interaction.isCommand()) return;
10 if (interaction.commandName === 'ping') {
11 await interaction.reply('Pong!');
12 }
13});
14
15client.login('token');
1import discord
2from discord.ext import commands
3
4client = commands.Bot(command_prefix ='')
5
6@client.event
7async def on_ready(): #Bot on
8 print ("Player One, Ready")
9
10@client.event
11async def on_message(ctx):
12 #code
13
14client.run('TOKEN')
15