2017-03-22 135 views
0

我想讓我的機器人發送一條消息,但它經常發送兩條相同的消息(即使當我只希望它只顯示一次時也重複該消息),但這隻發生在我能說的從使用機器人。這是我的代碼:Discord.py發送消息兩次

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() 

回答

0

那麼,你正在做一個函數send_message()。它可能會調用discord.py中的方法以及代碼中的函數。刪除你的功能,因爲它不會爲你做任何事情。