repl.it
@21natzil/

discordy

Python

An attempt at making a discord library

fork
loading
Files
  • main.py
  • discordy
  • setup.py
  • test.png
  • Packager files
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from discordy.objects.snowflake import SnowflakeObj
s = SnowflakeObj(508356525983793152)
print(s.timestamp)

import time

import logging

logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

try:
    import http.client as http_client
except ImportError:
    # Python 2
    import httplib as http_client
http_client.HTTPConnection.debuglevel = 1

from discordy.api.channels import Channel
from discordy.client import Client
from discordy.api.guild import Guild
from discordy.objects.gateway.events import ReadyEvent, MessageCreateEvent, MessageReactionAddEvent
from discordy.objects.gateway.commands import UpdateStatusObj
from discordy.objects.gateway.presence import ActivityObj

client = Client()


class OnReady(ReadyEvent):

  def run(self):
    print("Ready!", self.user.username)
    presence = UpdateStatusObj(
      game = ActivityObj(
        name="Testing Presence!",
        type=0
      ),
      status = "online",
      ask = False
    )
    client.set_presence(presence)


class OnMessage(MessageCreateEvent):

  def run(self):
    if self.content.startswith("=ping"):
      print("Pong!")
      self.respond("Pong!")
    elif self.content.startswith("=test_img"):
      with open('test.png', 'rb') as file:
        self.respond('IMG', stream=('test.png', file))
    elif self.content.startswith('=test_reaction'):
      msg = self.respond("Test Reaction: 🔥")
      msg.react("🔥")
      time.sleep(5)
      msg.remove_reaction("🔥")
    elif self.content.startswith("=add_emoji"):
      name = '_'.join(self.content.split(' ')[1:])
      if not name:
        self.respond("Please enter a name for emoji")
      try:
        print(type(self.author.name), type(self.author.id))
        reaction = client.emitter.wait_for(MessageReactionAddEvent, 8, message_id=self.id, user_id=self.author.id)
      except TimeoutError:
        return self.respond("Sorry, you took to long to react!")
      reaction.emoji.add_to("446740837515395072", "test")
      self.respond("Added the emoji!")
    elif self.content.startswith("=make_guild"):
      name = ' '.join(self.content.split(' ')[1:])
      new_guild = Guild.create_guild(name)
      inv = Channel.create_invite(new_guild.system_channel_id, 1)
      print(inv.code)

    elif self.content.startswith("=invite"):
      guild_id = self.content.split(' ')[1]
      inv = Guild.get_invites(guild_id)
      self.respond(','.join([i.code for i in inv]))
    elif self.content.startswith('=count'):
      _, msg_id, emoji = self.content.split(' ')
      e_name = emoji[1:len(emoji)-1]
      msg = Channel.get_message(self.channel_id, msg_id)
      self.respond(str(len(msg.get_reactions(e_name))))

    elif self.content.startswith('=echo'):
      msg = ' '.join(self.content.split(' ')[1:])
      member = self.channel
      # print(member.guild_id)
      print(dir(member))
      perms = self.channel.apply_permission(member)
      if perms & 0x00000008 == 0x00000008:
        self.respond(msg)
      else:
        self.respond("Invalid Permissions")

client.emitter.add_listener(OnReady)
client.emitter.add_listener(OnMessage)

client.run_forever()