bot.py

 avatar
unknown
plain_text
2 years ago
3.1 kB
7
Indexable
import discord
import requests
import json
import asyncio
import datetime
import time
from discord.ext import commands
 
intents = discord.Intents.default()
intents.message_content = True
bot=commands.Bot(command_prefix="--",case_insensitive=True,intents=intents)
bot.remove_command("help")
client = discord.Client(intents=discord.Intents.all())
client = commands.Bot(command_prefix='!', intents=discord.Intents.all())
 
def get_score():
    leaderboard = ''
    id = 1
    response = requests.get("http://127.0.0.1:8000/api/score/leaderboard/")
    json_data = json.loads(response.text)
 
    for item in json_data:
        leaderboard += str(id) + ' ' + item['name'] + ":white_small_square:  Points: " + str(item['points']) + "\n"
        id += 1
 
    return(leaderboard)
 
def update_score(user, points):
 
    url = 'http://127.0.0.1:8000/api/score/update/'
    new_score = {'name': user, 'points': points}
    x = requests.post(url, data = new_score)
 
    return
 
def get_question():
    qs = ''
    id = 1
    answer = 0
    points = 0
    response = requests.get("http://127.0.0.1:8000/api/random/")
    json_data = json.loads(response.text)
    qs += " \n"  #  qs += "Question: \n"
    qs += json_data[0]['title'] + "\n" + "\n"
 
    for item in json_data[0]['answer']:
        qs += str(id) + ". " + item['answer'] + "\n"
 
        if item['is_correct']:
            answer = id
       
        id += 1
   
        points = json_data[0]['points']
   
    return(qs, answer, points)
 
 
@client.event
async def on_message(message):
   
    channelname = client.get_channel(559320337540448258)
 
    if message.author == client.user:
        return
 
   
    if message.content.startswith('!hof'):
        leaderboard = get_score()
        embedVar = discord.Embed(title="|              *HallOfFans*              |", description="", color=0x00ff00)
        embedVar.add_field(name="Top of active fans: \n", value=leaderboard, inline=False)
        await message.channel.send(embed=embedVar)
 
    if message.content.startswith('!trivia'):
        null = "`"
        qs, answer, points = get_question()
        embedVar2 = discord.Embed(title="Question is:", description=qs, color=0x353031)
        await message.channel.send(embed=embedVar2)
 
 
        def check(m):
            #return m.author == message.author and m.content.isdigit() # answer only to the one who ask
            return m.content.isdigit() == True
 
        try:
            guess = await client.wait_for('message', check=check, timeout=15.0)
 
        except asyncio.TimeoutError:
            return await message.channel.send('Time is up!')
 
        if int(guess.content) == answer:
            user = guess.author
            msg = str(guess.author.name) + ', good answer! You got ' + str(points) + ' points'
            await message.channel.send(msg)
            update_score(user, points)
 
        else:
            await message.channel.send('Wrong answer!')
 
             
client.run('xxxxxxxxxxx)
Editor is loading...