建資 陳泓宇
利用程式碼來操控的機器人帳號
使用者發送訊息
discord 伺服器
機器人偵測訊息
replit 上的 discord API
處裡訊息
機器人發送訊息
discord 伺服器
使用者接收訊息
code 區
檔案區
Text
執行程式
import os
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix = "!")
token = os.environ['TOKEN']
bot.run(token)
import os
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix = "!")
@bot.command()
async def hello(ctx):
await ctx.send("早安")
token = os.environ['TOKEN']
bot.run(token)
輸入!name時 發送你的名字
import os
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix = "!")
@bot.command()
async def hello(ctx):
await ctx.send("hello")
@bot.command()
async def ping(ctx):
await ctx.send("pong")
@bot.command()
async def awa(ctx):
await ctx.send("ouo")
await ctx.send("uwu")
token = os.environ['TOKEN']
bot.run(token)
@bot.command()
async def repeat(ctx, a, b):
await ctx.send(a)
await ctx.send(b)
@bot.command()
async def repeat(ctx, a:str, b:str):
await ctx.send(a)
await ctx.send(b)
@bot.command()
async def add(ctx, a:int, b:int):
await ctx.send(a+b)
await ctx.send(a-b)
name = chenhowie
num = 1234
string = f"hello {name} {num}"
print(string) //hello chenhowie 1234
大括號中放變數
@bot.command()
async def pic(ctx):
picture = discord.File("example.png")
await ctx.send(file=picture)
輸入 !pic 生成一張照片(隨便一張都可以)
@bot.command()
async def embed(ctx):
embed=discord.Embed(title="標題", description="敘述", color=0x0084ff)
embed.add_field(name="ouo", value="1", inline=False)
embed.add_field(name="awa", value="2", inline=False)
embed.add_field(name="uwu", value="3", inline=False)
await ctx.send(embed=embed)
輸入!embed 標題 敘述 名字 數字 生成一個embed如下
@bot.command()
async def emoji(ctx):
await ctx.send("<:angrymention:918163460502728724>")
頭像右鍵點copy ID
@bot.command()
async def mention(ctx):
await ctx.send("<@!742373231025193000>")
@bot.command()
async def hello(ctx):
await ctx.reply("hi")
很多個人一起打指令時會混淆 不知道機器人在回覆誰
很多人一起打指令可以很清楚的知道機器人在回覆誰
import random
@bot.command()
async def hello(ctx):
c = random.randint(0, 2)
if c == 0:
await ctx.send("hi")
elif c == 1:
await ctx.send("早安")
elif c == 2:
await ctx.send("hello")
from flask import Flask
from threading import Thread
app = Flask('')
@app.route('/')
def home():
return "Hello. I am alive!"
def run():
app.run(host='0.0.0.0',port=8080)
def keep_alive():
t = Thread(target=run)
t.start()
import os
import discord
from discord.ext import commands
from keep_alive import keep_alive
bot = commands.Bot(command_prefix="!")
keep_alive()
token = os.environ['TOKEN']
bot.run(token)