xxxxxxxxxx
import discord
from discord.ext import commands
TOKEN = 'Your Discord Bot Token'
# Create an instance of the bot
bot = commands.Bot(command_prefix='!')
# Event to perform when the bot is ready to start interacting with the server
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
# Command to say hello
@bot.command()
async def hello(ctx):
await ctx.send('Hello, I am your Discord bot!')
# Run the bot
bot.run(TOKEN)
xxxxxxxxxx
import discord
from discord.ext import commands
client = commands.Bot(command_prefix=".")
@client.event
async def on_ready():
print("Ready!")
bot.run("TOKEN")
xxxxxxxxxx
from cmath import log
from tracemalloc import start
import discord
from discord.ext import commands
from requests import request
import os
from random import seed
from random import randint
intents = discord.Intents().all();
client = commands.Bot(command_prefix="!"); // set the command prefix for the bot
game = discord.Game(name="YOUR-ACTIVITY"); // set the bot actual activity
@client.event
async def on_ready():
await client.change_presence(activity=game); // Set the activity of the bot when ready
print("The bot is ready!") // Send a msg in your terminal to say that the bot is ready
@client.command()
@commands.is_owner()
async def shutdown(context): // Shutdown the bot (Only used by the bot owner) -> Respond to !shutdown command
exit()
@client.command()
async def hello(ctx): // Send "Hi" message to the channel (Usable by anyone) -> Respond to !hello command
await ctx.send("Hi")
@client.event
async def on_message(message):
if (message.content.startswith("!") == False and message.author != client.user and (message.channel.name == "bot")): // Juste a double check that the bot isn't responding to himself, that the msg is not a command and that the bot only respond in "bot" channel
if message.content.startswith("!") == True: // If the message is a command
await client.process_commands(message) // Process the command
token = "YOUR-TOKEN"
client.run(token)
xxxxxxxxxx
import discord
from discord.ext import commands
# Bot setup
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
bot = commands.Bot(command_prefix='!', intents=intents)
# Event: bot is ready
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name} ({bot.user.id})')
# Event: bot receives a message
@bot.event
async def on_message(message):
if message.author == bot.user:
return # Ignore messages from the bot itself
if message.content.startswith('!hello'):
await message.channel.send('Hello!')
# Add more custom bot commands and functionality here
# Run the bot
bot.run('YOUR_BOT_TOKEN')
xxxxxxxxxx
import discord
from discord.ext import commands
bot = commands.Bot("!")
@bot.command()
async def test(ctx):
print('hello')
bot.run('token')
xxxxxxxxxx
from multiprocessing.connection import Client
from urllib import response
import discord
import random
import os
TOKEN = 'YOUR TOKEN'
client = discord.Client()
@client.event
async def on_ready():
print('I have logged in as {0.user} '.format(client))
@client.event
async def on_message(message):
username = str(message.author).split('#')[0]
user_message = str(message.content)
channel = str(message.channel.name)
print(f'{username}: {user_message} ({channel})')
if message.author == client.user:
return
if message.channel.name == 'YOUR CHANNEL':
if user_message.lower() == 'hello':
await message.channel.send(f'Hello {username}!')
return
elif user_message.lower() == 'bye':
await message.channel.send(f'See you later {username}.')
return
if message.channel.name == 'YOUR CHANNEL':
if user_message.lower() == '.random':
response = f'This is your random number: {random.randrange(1000000000)}'
await message.channel.send(response)
return
elif user_message.lower() == '.help':
response = f'This is a list of all availabe commands: 1. .random (Displays you a random number) 2. .help (Displays you a list with all avaiable commands)'
await message.channel.send(response)
return
elif user_message.lower() == '.ping':
response = f'**Pong**'
await message.channel.send(response)
return
client.run(TOKEN)
xxxxxxxxxx
q_list = [
'What is your favorite color?',
'Is the Sky Blue?',
'Am I the best bot ever?'
]
a_list = []
@client.command(aliases=['staff-application'])
async def staff_application(ctx):
a_list = []
submit_channel = client.get_channel(<your channel id>)
channel = await ctx.author.create_dm()
def check(m):
return m.content is not None and m.channel == channel
for question in q_list:
sleep(.5)
await channel.send(question)
msg = await client.wait_for('message', check=check)
a_list.append(msg.content)
submit_wait = True
while submit_wait:
await channel.send('End of questions - "submit" to finish')
msg = await client.wait_for('message', check=check)
if "submit" in msg.content.lower():
submit_wait = False
answers = "\n".join(f'{a}. {b}' for a, b in enumerate(a_list, 1))
submit_msg = f'Application from {msg.author} \nThe answers are:\n{answers}'
await submit_channel.send(submit_msg)
xxxxxxxxxx
import discord
from discord.ext import commands
TOKEN = 'your_discord_bot_token'
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name} ({bot.user.id})')
print('------')
@bot.command()
async def hello(ctx):
await ctx.send(f'Hello, {ctx.author.name}!')
bot.run(TOKEN)
xxxxxxxxxx
from nextcord.ext import commands
bot = commands.Bot(command_prefix='$')
@bot.command()
async def test(ctx):
pass
# or:
@commands.command()
async def test(ctx):
pass
bot.add_command(test)
xxxxxxxxxx
#導入Discord.py
#安裝模組語法 python
#python3 -m pip install discord.py
import discord
import signal
import datetime
import time
import winsound
import random
import string
import logging
import sys
import datetime
import base64
#client是我們與Discord連結的橋樑
client = discord.Client()
#調用event函式庫
@client.event
#當機器人完成啟動時
async def on_ready():
print('吳鑽登入身份:',client.user)
#調用 event 函式庫
@client.event
#當有訊息時
async def on_message(message):
#排除自己的訊息,避免陷入無限循環
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('wu!!say'):
#刪除傳送者的訊息
await message.delete()
#分割訊息成兩份
tmp = message.content.split(" ",1)
#如果分割後串列長度只有1
if len(tmp) == 1:
await message.channel.send("快設")
else:
await message.channel.send(tmp[1])
if message.content.startswith('wu!設置鬧鐘'):
#刪除傳送者的訊息
await message.delete()
#分割訊息成兩份
tmp = message.content.split(" ")
time.sleep(int(tmp[1]))
await message.channel.send(f" {message.author.mention} 鬧鐘響了 已經過了"+tmp[1]+"秒喔 ~ , ouo")
#如果分割後串列長度只有1
if len(tmp) == 1:
await message.channel.send("快設鬧鐘")
if message.content == 'wu!server':
guilds = await client.fetch_guilds(limit=150).flatten()
for i in guilds:
await message.channel.send(i.name)
#這邊設定機器人的狀態
#discord.Status.<狀態>,可以是online(上線),offline(下線),idle(閒置),dnd(請勿打擾),invisible(隱身)
status_w = discord.Status.online
#這邊設定機器當前的狀態文字
#type可以是playing(遊玩中)、streaming(直撥中)、listening(聆聽中)、watching(觀看中)、custom(自定義)
activity_w = discord.Activity(type=discord.ActivityType.streaming, name="吳鑽寫程式!")
await client.change_presence(status= status_w, activity=activity_w)
def addStringcolor(SourceString,sing):
NewString="```diff\n"+sing+SourceString+"\n```"
return NewString
def addStringcolorStart(SourceString,sing):
NewString="```diff\n"+sing+SourceString+"\n"
return NewString
def addStringcolorjson(SourceString,sing):
NewString="```json\n"+sing+SourceString+"```"
return NewString
def addStringcolorStartjson(SourceString,sing):
NewString="```json\n"+sing+SourceString+"\n"
return NewString
def addStringcolormd(SourceString,sing):
NewString="```md\n"+sing+SourceString+"```"
return NewString
def addStringcolorStartmd(SourceString,sing):
NewString="```md\n"+sing+SourceString+"\n"
return NewString
def addStringcolormid(SourceString,NewString,sing):
retrunNewString=SourceString+sing+NewString+"\n"
return retrunNewString
def addStringcolorEnd(SourceString):
NewString=SourceString+"\n```"
return NewString
client.run('token') #TOKEN在剛剛Discord Developer那邊「BOT」頁面裡面