Skip to content

Instantly share code, notes, and snippets.

@Robijnvogel
Created November 11, 2017 21:05
Show Gist options
  • Save Robijnvogel/8a0a2066b159fd985d255a797d5905ac to your computer and use it in GitHub Desktop.
Save Robijnvogel/8a0a2066b159fd985d255a797d5905ac to your computer and use it in GitHub Desktop.
import discord
import logging
import asyncio
import os
import platform
#constants
nowPlayingFilePath = r"filepath"
#logging stuff
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
#discord client
client = discord.Client()
def readFromFile(path_to_file):
with open (path_to_file, "r") as myfile:
data = myfile.read()
print('read data from file: ' + data)
return data
#fetching the date a file was last modified in nanoseconds
def modification_date(path_to_file): #source: https://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python
"""
Try to get the date that a file was last modified, falling back to when it was created if that isn't possible.
See http://stackoverflow.com/a/39501288/1709587 for explanation. <-- ?¿?
"""
if platform.system() == 'Windows':
return os.path.getmtime(path_to_file)
else:
stat = os.stat(path_to_file)
try:
return stat.st_mtime
except AttributeError:
# We're probably on Linux. This should probably never be run and if it is, it is almost guaranteed to throw some exception
return stat.st_birthtime
#checking for file change, if so, update the play text
async def my_background_task():
await client.wait_until_ready()
timeLastModified = 0
actualTimeLastModified = 0
nowPlayingMessage = ""
channel = discord.Object(id='memesnspam')
while not client.is_closed:
actualTimeLastModified = modification_date(nowPlayingFilePath)
if timeLastModified != actualTimeLastModified:
timeLastModified = actualTimeLastModified
nowPlayingMessage = 'hello' #readFromFile(nowPlayingFilePath)
await client.change_presence(game=discord.Game(name=nowPlayingMessage))
await asyncio.sleep(5) # task runs every 5 seconds
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.loop.create_task(my_background_task())
client.run('token')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment