Discord Python API包装器“ Send_message”什么都不做?

时间:2018-09-02 18:11:09

标签: python discord discord.py

因此,我编写了一些代码,这些代码从Google日历中获取事件并进行组织,然后将它们发布到不和谐的地方。主要问题是:client.send_message(client.get_channel('483988804370169857'),"None") 在@ client.event中使用此代码时,它可以正常工作,当我将其放入常规函数中时,问题就开始了。我需要它具有正常功能,因此它每24小时运行一次。没有错误,什么都没有。该代码仅通过该函数,并且不会发送消息。我该如何解决?

from __future__ import print_function
import json
import datetime
import discord
from discord.ext import commands
from datetime import timedelta
import gspread
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from discord.ext import commands
import threading



SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'


client = discord.Client()



def Hw():
    print("HW")
    now = datetime.datetime.now()
    run_at = now + timedelta(hours=3)
    delay = (run_at - now).total_seconds()
    #Begin Google Calander API
    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('calendar', 'v3', http=creds.authorize(Http()))
    now = datetime.datetime.utcnow().isoformat() + 'Z' 
        #Retrieve all Events
    events_result = service.events().list(calendarId='CENSORED', timeMin=now,maxResults=250, singleEvents=True,orderBy='startTime').execute()
    events = events_result.get('items', [])

        #Currentmode stores if searching for tommorow, the week, or other (1,2,3)
    Currentmode = 0
    UMode=1
    none = True
        #Gets end of week
    dt = datetime.datetime.now()
    startweek = dt - timedelta(days=dt.weekday())
    endweek = startweek + timedelta(days=6)
    dtstring = str(dt.date())
    TheMessages = "**" + dtstring + " Report**"
    client.send_message(client.get_channel('483988804370169857'),TheMessages)
    if not events:
        client.send_message(client.get_channel('483988804370169857'), 'No upcoming events found.')
    for event in events:

        if Currentmode == 0:
            client.send_message(client.get_channel('483988804370169857'),"*Due Tommorow*")
            Currentmode = 1  

        thestr = event['start'].get('dateTime')

        count = 0
        count2 = 0
        for x in thestr:
            count += 1
            if x == "-":
                count2 +=1
                if count2 == 3:
                    break
        count = count - 1
        thestr = thestr[0:count]


        start = datetime.datetime.strptime(thestr, "%Y-%m-%dT%H:%M:%S")

        if (start - dt).days <= 7 and Currentmode == 1:
            if UMode == 1:
                client.send_message(client.get_channel('483988804370169857'),"None")
            client.send_message(client.get_channel('483988804370169857'),"*Due in the Next 7 Days*")
            UMode = 2
            Currentmode = 2
        elif (start - dt).days >= 7 and (Currentmode == 1 or Currentmode == 2):
            if UMode == 1:
                client.send_message(client.get_channel('483988804370169857'),"None")
                client.send_message(client.get_channel('483988804370169857'),"*Due in the Next 7 Days*")
                client.send_message(client.get_channel('483988804370169857'),"None")
            client.send_message(client.get_channel('483988804370169857'),"*Longterm*") 
            Currentmode = 3
            UMode = 3
        FirstMessage = str(start.date())
        SecondMessage = event['summary']
        ThirdMessage= FirstMessage + " " + SecondMessage
        client.send_message(client.get_channel('483988804370169857'),ThirdMessage)
@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('!hello'):
        msg = 'Hello {0.author.mention}'.format(message)
        await client.send_message(message.channel, msg)
@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    now = datetime.datetime.now()
    run_at = now + timedelta(seconds=1)
    delay = (run_at - now).total_seconds()
    threading.Timer(delay, Hw).start()



#Run client and start daily timer       
client.run('CENSORED')

0 个答案:

没有答案