Fortune teller Bot ( แม่หมอสายมู )
Random string from prefilled array, have sound effect, have light effect.
import discord
from discord.ext import commands
import time
import random
import requests
#เรียกใช้งาน library ที่จำเป็น
client = commands.Bot(command_prefix = '!ft ',intents=discord.Intents.all())
timenow = time.localtime()
fortune_file = open("fortune.txt", "r")
fortune_lines = fortune_file.read().split('\n')
#อ่านไฟล์ และ เก็บไว้เป็น Array
command_file = open("command.txt", "r")
command_lines = command_file.read().split('\n')
list_comm = "\n".join(command_lines)
#อ่านไฟล์ แล้วแปลงเป็น String
todayF = random.choice(fortune_lines)
iot_ip = "http://172.20.10.2"
@client.event
async def on_ready():
print("The bot ready to forecast!!")
print("---------------------------")
@client.command()
async def command(ctx):
await ctx.send(list_comm)
@client.command()
async def hello(ctx):
await ctx.send("Hello, Are you ready to know your fortune today ? :raised_back_of_hand:")
@client.command()
async def fortune(ctx):
# call light&sound api
for i in range(5):
try:
requests.get(iot_ip + "/on?red=1&blue=0&green=0")
requests.get(iot_ip + "/play?hz=261&duration=0.1")
time.sleep(0.1)
requests.get(iot_ip + "/on?red=0&blue=0&green=1")
requests.get(iot_ip + "/play?hz=293&duration=0.1")
time.sleep(0.1)
requests.get(iot_ip + "/on?red=0&blue=1&green=0")
requests.get(iot_ip + "/play?hz=329&duration=0.1")
time.sleep(0.1)
except:
print("Failed to call IOT API!")
try:
requests.get(iot_ip + "/play?hz=350&duration=0.5")
except:
print("Failed to call IOT API!")
await ctx.send(newfortuneFix())
@client.command()
async def color(ctx):
await ctx.send('Color of fortune in 2024 by doctor Kai',file=discord.File('fortune color.png'))
def newfortuneFix():
return random.choice(fortune_lines)
def newfortune():
global todayF
print("Your new fortune coming right up")
todayF = random.choice(fortune_lines)
print(todayF)
return todayF
# function รับคำขอพรวันใหม่
client.run('MTE5Mzc2MjIxMzYxNDM0MjI5NQ.GIA8dH.cKXYVEq0EULsWe5EkgAnxDhQKEFgYq_w-lSRjs') #ใส่ TOKEN ที่นี่