Discord.py两次发送消息

时间:2017-03-22 23:21:24

标签: python pygame python-asyncio discord

我希望我的机器人发送一条消息,但它经常发送两条相同的消息(即使我只希望它显示一次也重复该消息),这只是从我使用机器人可以告诉的内容中随机发生的。这是我的代码:

import discord, pygame, time, asyncio
import random
from threading import Thread


client = discord.Client()
song = {0:"LOL",1:"https://www.youtube.com/watch?v=cUbFzEMQ2Fs",2:"https://www.youtube.com/watch?v=YlomIQF2zbI",3:"https://www.youtube.com/watch?v=2yhhK_2HZzQ" ,4:"https://www.youtube.com/playlist?list=PLge2NVqdJnpDoUhLAdnaQiUCylTnMpWJQ",5:"https://www.youtube.com/watch?v=O6NvsM49N6w&index=62"}
songs = -1
songnum = 1
songmax = 3

songname = {1: "Run", 2: "Windows down", 3:"Remember when",4:"Playlist",5:"How to be a heart breaker"}
songmax = len(songname)
pygame.init()      # put these in the beginning
gameDisplay = pygame.display.set_mode((500, 500))
white = (255, 255, 255)

clock = pygame.time.Clock()

green = (0, 255, 0)
red = (255, 0, 0)
black = (0, 0, 0)
lightred = (230, 0, 0)




async def send_message(msg,channel):
    await client.send_message(client.get_channel(channel), msg)     

def text(msg,x,y,color):
    myfont = pygame.font.SysFont("monospace", 15)


    label = myfont.render(msg, 1, color)
    gameDisplay.blit(label, (x, y))




def button(x, y, w, h, ac, ic, songs):
    global songnum
    global songmax

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if x + w > mouse[0] > x and y + h > mouse[1] > y:

        pygame.draw.rect(gameDisplay, ac, (x, y, w, h))

        if click[0] == 1:

            if songs == 0:
                if songnum == songmax:
                    songnum = 1
                else:
                    songnum += 1
            elif songs == 1:
                if songnum == 1:
                    songnum = songmax
                else:
                    songnum += -1
            elif songs == 2:
                asyncio.ensure_future(send_message(msg=";;play " + (song[songnum]), channel="channel id"))
                asyncio.ensure_future(send_message(msg="Added to queue: " + (songname[songnum]), channel="channel id"))
                print("Added:"+(songname[songnum]))
            elif songs == 3:
                asyncio.ensure_future(send_message(msg=";;skip" , channel="channel id"))
                asyncio.ensure_future(send_message(msg="Skipped a song", channel="channel id"))
                print("Skipped")
            elif songs == 4:
                asyncio.ensure_future(send_message(msg=";;stop" , channel="293852321912455169"))
                asyncio.ensure_future(send_message(msg="Stopped the music", channel="259075498213113856"))
                print("Stopped music")


    else:
        pygame.draw.rect(gameDisplay, ic, (x, y, w, h))


def run_gui():
    gameDisplay.fill(white)
    done = True
    global songname
    while done:
        pygame.display.update()

        for event in pygame.event.get():
            gameDisplay.fill(white)
            button(400, 300, 75, 100, red, black, 0)
            text("Up",400,300,white)
            button(400, 100, 75, 100, red, black, 1)
            text("Down", 400, 100,white)
            button(50, 50, 75, 100, red, black, 2)
            text("Play", 50, 50, white)
            text("Music", 50, 65, white)
            button(50, 350, 75, 100, red, black, 3)
            text("Skip", 50, 350, white)
            button(150, 350, 75, 100, red, black, 4)
            text("Stop", 150, 350, white)
            text(str(songname[songnum]), 150, 250, black)



        clock.tick(60)

def run_bot():
    client.run('token')

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

    channel = client.get_channel('id')      
    #259075498213113857 for gernal
    await client.join_voice_channel(channel)
    print('Bot should joined the Channel')



Thread(target=run_bot).start()
run_gui()

1 个答案:

答案 0 :(得分:0)

嗯,你正在创建一个名为send_message()的函数。它可能会调用discord.py中的方法和代码中的函数。删除你的功能,因为它不会为你做任何事情。